Commit 3a146a06 by Qiang Xue

Refactored view renderers.

parent 385f711f
......@@ -70,10 +70,25 @@ class View extends Component
*/
public $params;
/**
* @var ViewRenderer|array the view renderer object or the configuration array for
* creating the view renderer. If not set, view files will be treated as normal PHP files.
* @var array a list of available renderers indexed by their corresponding supported file extensions.
* Each renderer may be a view renderer object or the configuration for creating the renderer object.
* For example,
*
* ~~~
* array(
* 'tpl' => array(
* 'class' => 'yii\renderers\SmartyRenderer',
* ),
* 'twig' => array(
* 'class' => 'yii\renderers\TwigRenderer',
* ),
* )
* ~~~
*
* If no renderer is available for the given view file, the view file will be treated as a normal PHP
* and rendered via [[renderPhpFile()]].
*/
public $renderer;
public $renderers = array();
/**
* @var Theme|array the theme object or the configuration array for creating the theme object.
* If not set, it means theming is not enabled.
......@@ -152,9 +167,6 @@ class View extends Component
public function init()
{
parent::init();
if (is_array($this->renderer)) {
$this->renderer = Yii::createObject($this->renderer);
}
if (is_array($this->theme)) {
$this->theme = Yii::createObject($this->theme);
}
......@@ -226,8 +238,14 @@ class View extends Component
$output = '';
if ($this->beforeRender($viewFile)) {
if ($this->renderer !== null) {
$output = $this->renderer->render($this, $viewFile, $params);
$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
if (isset($this->renderers[$ext])) {
if (is_array($this->renderers[$ext])) {
$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
}
/** @var ViewRenderer $renderer */
$renderer = $this->renderers[$ext];
$output = $renderer->render($this, $viewFile, $params);
} else {
$output = $this->renderPhpFile($viewFile, $params);
}
......
<?php
/**
* Composite view renderer class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\renderers;
use Yii;
use yii\base\View;
use yii\base\ViewRenderer;
/**
* CompositeViewRenderer allows you to use multiple view renderers in a single
* application.
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class CompositeViewRenderer extends ViewRenderer
{
/**
* @var array a config array with the view renderer objects or the configuration arrays for
* creating the view renderers indexed by file extensions.
*/
public $renderers = array();
/**
* Renders a view file.
*
* This method is invoked by [[View]] whenever it tries to render a view.
* Child classes must implement this method to render the given view file.
*
* @param View $view the view object used for rendering the file.
* @param string $file the view file.
* @param array $params the parameters to be passed to the view file.
*
* @return string the rendering result
*/
public function render($view, $file, $params)
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext === 'php' || !isset($this->renderers[$ext])) {
return $view->renderPhpFile($file, $params);
}
else {
if (is_array($this->renderers[$ext])) {
$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
}
return $this->renderers[$ext]->render($view, $file, $params);
}
}
}
\ No newline at end of file
......@@ -9,8 +9,10 @@
namespace yii\renderers;
use \yii\base\View;
use \yii\base\ViewRenderer;
use Yii;
use Smarty;
use yii\base\View;
use yii\base\ViewRenderer;
/**
* SmartyViewRenderer allows you to use Smarty templates in views.
......@@ -21,34 +23,31 @@ use \yii\base\ViewRenderer;
class SmartyViewRenderer extends ViewRenderer
{
/**
* @var string alias pointing to where Smarty code is located.
* @var string the directory or path alias pointing to where Smarty code is located.
*/
public $smartyDir = '@app/vendors/Smarty';
public $smartyPath = '@app/vendors/Smarty';
/**
* @var string alias pointing to where Smarty cache will be stored.
* @var string the directory or path alias pointing to where Smarty cache will be stored.
*/
public $cacheDir = '@app/runtime/Smarty/cache';
public $cachePath = '@app/runtime/Smarty/cache';
/**
* @var string alias pointing to where Smarty compiled teamplates will be stored.
* @var string the directory or path alias pointing to where Smarty compiled templates will be stored.
*/
public $compileDir = '@app/runtime/Smarty/compile';
public $compilePath = '@app/runtime/Smarty/compile';
/**
* @var string file extension to use for template files
* @var Smarty
*/
public $fileExtension = 'tpl';
/** @var \Smarty */
protected $_smarty;
public $smarty;
public function init()
{
require_once(\Yii::getAlias($this->smartyDir).'/Smarty.class.php');
$this->_smarty = new \Smarty();
$this->_smarty->setCompileDir(\Yii::getAlias($this->compileDir));
$this->_smarty->setCacheDir(\Yii::getAlias($this->cacheDir));
require_once(Yii::getAlias($this->smartyPath) . '/Smarty.class.php');
$this->smarty = new Smarty();
$this->smarty->setCompileDir(Yii::getAlias($this->compilePath));
$this->smarty->setCacheDir(Yii::getAlias($this->cachePath));
}
/**
......@@ -66,13 +65,8 @@ class SmartyViewRenderer extends ViewRenderer
public function render($view, $file, $params)
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext === $this->fileExtension) {
/** @var \Smarty_Internal_Template $template */
$template = $this->_smarty->createTemplate($file, null, null, $params, true);
$template = $this->smarty->createTemplate($file, null, null, $params, true);
return $template->fetch();
}
else {
return $view->renderPhpFile($file, $params);
}
}
}
\ No newline at end of file
......@@ -9,8 +9,9 @@
namespace yii\renderers;
use \yii\base\View;
use \yii\base\ViewRenderer;
use Yii;
use yii\base\View;
use yii\base\ViewRenderer;
/**
* TwigViewRenderer allows you to use Twig templates in views.
......@@ -21,19 +22,14 @@ use \yii\base\ViewRenderer;
class TwigViewRenderer extends ViewRenderer
{
/**
* @var string alias pointing to where Twig code is located.
* @var string the directory or path alias pointing to where Twig code is located.
*/
public $twigDir = '@app/vendors/Twig';
public $twigPath = '@app/vendors/Twig';
/**
* @var string alias pointing to where Twig cache will be stored.
* @var string the directory or path alias pointing to where Twig cache will be stored.
*/
public $cacheDir = '@app/runtime/Twig/cache';
/**
* @var string file extension to use for template files.
*/
public $fileExtension = 'twig';
public $cachePath = '@app/runtime/Twig/cache';
/**
* @var array Twig options
......@@ -44,16 +40,16 @@ class TwigViewRenderer extends ViewRenderer
/**
* @var \Twig_Environment
*/
protected $_twig;
public $twig;
public function init()
{
\Yii::setAlias('@Twig', $this->twigDir);
Yii::setAlias('@Twig', $this->twigPath);
$loader = new \Twig_Loader_String();
$this->_twig = new \Twig_Environment($loader, array_merge(array(
'cache' => \Yii::getAlias($this->cacheDir),
$this->twig = new \Twig_Environment($loader, array_merge(array(
'cache' => Yii::getAlias($this->cachePath),
), $this->options));
}
......@@ -71,12 +67,6 @@ class TwigViewRenderer extends ViewRenderer
*/
public function render($view, $file, $params)
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext === $this->fileExtension) {
return $this->_twig->render(file_get_contents($file), $params);
}
else {
return $view->renderPhpFile($file, $params);
}
return $this->twig->render(file_get_contents($file), $params);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment