From 3a146a0662435e50707cda8e50befeac847117d3 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 27 Apr 2013 13:30:25 -0400 Subject: [PATCH] Refactored view renderers. --- framework/base/View.php | 34 ++++++++++++---- framework/renderers/CompositeViewRenderer.php | 56 --------------------------- framework/renderers/SmartyViewRenderer.php | 44 +++++++++------------ framework/renderers/TwigViewRenderer.php | 34 ++++++---------- 4 files changed, 57 insertions(+), 111 deletions(-) delete mode 100644 framework/renderers/CompositeViewRenderer.php diff --git a/framework/base/View.php b/framework/base/View.php index edb67be..a72982e 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -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); } diff --git a/framework/renderers/CompositeViewRenderer.php b/framework/renderers/CompositeViewRenderer.php deleted file mode 100644 index a7fbcf3..0000000 --- a/framework/renderers/CompositeViewRenderer.php +++ /dev/null @@ -1,56 +0,0 @@ - - * @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 diff --git a/framework/renderers/SmartyViewRenderer.php b/framework/renderers/SmartyViewRenderer.php index 92fa3f2..ab9125a 100644 --- a/framework/renderers/SmartyViewRenderer.php +++ b/framework/renderers/SmartyViewRenderer.php @@ -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); - return $template->fetch(); - } - else { - return $view->renderPhpFile($file, $params); - } + /** @var \Smarty_Internal_Template $template */ + $template = $this->smarty->createTemplate($file, null, null, $params, true); + return $template->fetch(); } } \ No newline at end of file diff --git a/framework/renderers/TwigViewRenderer.php b/framework/renderers/TwigViewRenderer.php index d08ec99..1c7951c 100644 --- a/framework/renderers/TwigViewRenderer.php +++ b/framework/renderers/TwigViewRenderer.php @@ -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); } }