From bb5962f9f596066faa617aa021734172cae0f0f1 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 8 Feb 2013 03:23:20 +0400 Subject: [PATCH] Basic Smarty and Twig view renderers, docs --- docs/view_renderers.md | 31 +++++++++++++++ framework/renderers/SmartyViewRenderer.php | 63 +++++++++++++++++++++++++++++ framework/renderers/TwigViewRenderer.php | 64 ++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 docs/view_renderers.md create mode 100644 framework/renderers/SmartyViewRenderer.php create mode 100644 framework/renderers/TwigViewRenderer.php diff --git a/docs/view_renderers.md b/docs/view_renderers.md new file mode 100644 index 0000000..bb72bba --- /dev/null +++ b/docs/view_renderers.md @@ -0,0 +1,31 @@ +Yii2 view renderers +=================== + +By default Yii uses PHP as template language but you can change it in your application. +The component responsible for rendering a view using custom template enging is +called `viewRenderer`. You can configure it as follows: + +```php +array( + 'components' => array( + 'viewRenderer' => array( + 'class' => 'yii\renderers\TwigViewRenderer', + // or 'class' => 'yii\renderers\SmartyViewRenderer', + ), + ), +) +``` + +Twig +---- + +In order to use Twig you need to put you templates in files with extension `.twig`. +Also you need to specify this extension explicitly when calling `$this->render()` +or `$this->renderPartial()` from your controller. + +Smarty +------ + +In order to use Smarty you need to put you templates in files with extension `.tpl`. +Also you need to specify this extension explicitly when calling `$this->render()` +or `$this->renderPartial()` from your controller. \ No newline at end of file diff --git a/framework/renderers/SmartyViewRenderer.php b/framework/renderers/SmartyViewRenderer.php new file mode 100644 index 0000000..c2bb58c --- /dev/null +++ b/framework/renderers/SmartyViewRenderer.php @@ -0,0 +1,63 @@ + + * @since 2.0 + */ +class SmartyViewRenderer extends ViewRenderer +{ + public $smartyDir = '@app/vendors/Smarty'; + public $cacheDir = '@app/runtime/Smarty/cache'; + public $compileDir = '@app/runtime/Smarty/compile'; + public $fileExtension = 'tpl'; + + /** @var \Smarty */ + protected $_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)); + } + + /** + * 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 === $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); + } + } +} \ No newline at end of file diff --git a/framework/renderers/TwigViewRenderer.php b/framework/renderers/TwigViewRenderer.php new file mode 100644 index 0000000..def90b4 --- /dev/null +++ b/framework/renderers/TwigViewRenderer.php @@ -0,0 +1,64 @@ + + * @since 2.0 + */ +class TwigViewRenderer extends ViewRenderer +{ + public $twigDir = '@app/vendors/Twig'; + public $cacheDir = '@app/runtime/Twig/cache'; + public $fileExtension = 'twig'; + + /** + * @var \Twig_Environment + */ + protected $_twig; + + public function init() + { + \Yii::setAlias('@Twig', $this->twigDir); + + $loader = new \Twig_Loader_String(); + $this->_twig = new \Twig_Environment($loader, array( + 'cache' => \Yii::getAlias($this->cacheDir), + )); + } + + /** + * 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 === $this->fileExtension) { + return $this->_twig->render(file_get_contents($file), $params); + } + else { + return $view->renderPhpFile($file, $params); + } + } +}