From 9a13ddb8a655828af6cd836b2fd1c5518f413455 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 24 May 2013 15:14:36 +0400 Subject: [PATCH] Moved Smarty and Twig renderers into extensions --- extensions/smarty/composer.json | 27 +++++++ extensions/smarty/yii/smarty/ViewRenderer.php | 99 ++++++++++++++++++++++++++ extensions/twig/composer.json | 27 +++++++ extensions/twig/yii/twig/ViewRenderer.php | 73 +++++++++++++++++++ framework/yii/renderers/SmartyViewRenderer.php | 99 -------------------------- framework/yii/renderers/TwigViewRenderer.php | 73 ------------------- 6 files changed, 226 insertions(+), 172 deletions(-) create mode 100644 extensions/smarty/composer.json create mode 100644 extensions/smarty/yii/smarty/ViewRenderer.php create mode 100644 extensions/twig/composer.json create mode 100644 extensions/twig/yii/twig/ViewRenderer.php delete mode 100644 framework/yii/renderers/SmartyViewRenderer.php delete mode 100644 framework/yii/renderers/TwigViewRenderer.php diff --git a/extensions/smarty/composer.json b/extensions/smarty/composer.json new file mode 100644 index 0000000..3a74f41 --- /dev/null +++ b/extensions/smarty/composer.json @@ -0,0 +1,27 @@ +{ + "name": "yiisoft/yii2-smarty", + "description": "The Smarty integration for the Yii framework", + "keywords": ["yii", "smarty", "renderer"], + "type": "library", + "license": "BSD-3-Clause", + "support": { + "issues": "https://github.com/yiisoft/yii2/issues?state=open", + "forum": "http://www.yiiframework.com/forum/", + "wiki": "http://www.yiiframework.com/wiki/", + "irc": "irc://irc.freenode.net/yii", + "source": "https://github.com/yiisoft/yii2" + }, + "authors": [ + { + "name": "Alenxader Makarov", + "email": "sam@rmcreative.ru" + } + ], + "minimum-stability": "dev", + "require": { + "yiisoft/yii2": "*" + }, + "autoload": { + "psr-0": { "yii\\smarty": "" } + } +} diff --git a/extensions/smarty/yii/smarty/ViewRenderer.php b/extensions/smarty/yii/smarty/ViewRenderer.php new file mode 100644 index 0000000..d8c5d30 --- /dev/null +++ b/extensions/smarty/yii/smarty/ViewRenderer.php @@ -0,0 +1,99 @@ + + * @since 2.0 + */ +class ViewRenderer extends BaseViewRenderer +{ + /** + * @var string the directory or path alias pointing to where Smarty cache will be stored. + */ + public $cachePath = '@app/runtime/Smarty/cache'; + + /** + * @var string the directory or path alias pointing to where Smarty compiled templates will be stored. + */ + public $compilePath = '@app/runtime/Smarty/compile'; + + /** + * @var Smarty + */ + public $smarty; + + public function init() + { + $this->smarty = new Smarty(); + $this->smarty->setCompileDir(Yii::getAlias($this->compilePath)); + $this->smarty->setCacheDir(Yii::getAlias($this->cachePath)); + + $this->smarty->registerPlugin('function', 'path', array($this, 'smarty_function_path')); + } + + /** + * Smarty template function to get a path for using in links + * + * Usage is the following: + * + * {path route='blog/view' alias=$post.alias user=$user.id} + * + * where route is Yii route and the rest of parameters are passed as is. + * + * @param $params + * @param \Smarty_Internal_Template $template + * + * @return string + */ + public function smarty_function_path($params, \Smarty_Internal_Template $template) + { + if (!isset($params['route'])) { + trigger_error("path: missing 'route' parameter"); + } + + array_unshift($params, $params['route']) ; + unset($params['route']); + + return Html::url($params); + } + + /** + * 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); + /** @var \Smarty_Internal_Template $template */ + $template = $this->smarty->createTemplate($file, null, null, $params, true); + + $template->assign('app', \Yii::$app); + $template->assign('this', $view); + + return $template->fetch(); + } +} diff --git a/extensions/twig/composer.json b/extensions/twig/composer.json new file mode 100644 index 0000000..624f8bd --- /dev/null +++ b/extensions/twig/composer.json @@ -0,0 +1,27 @@ +{ + "name": "yiisoft/yii2-twig", + "description": "The Twig integration for the Yii framework", + "keywords": ["yii", "twig", "renderer"], + "type": "library", + "license": "BSD-3-Clause", + "support": { + "issues": "https://github.com/yiisoft/yii2/issues?state=open", + "forum": "http://www.yiiframework.com/forum/", + "wiki": "http://www.yiiframework.com/wiki/", + "irc": "irc://irc.freenode.net/yii", + "source": "https://github.com/yiisoft/yii2" + }, + "authors": [ + { + "name": "Alenxader Makarov", + "email": "sam@rmcreative.ru" + } + ], + "minimum-stability": "dev", + "require": { + "yiisoft/yii2": "*" + }, + "autoload": { + "psr-0": { "yii\\twig": "" } + } +} diff --git a/extensions/twig/yii/twig/ViewRenderer.php b/extensions/twig/yii/twig/ViewRenderer.php new file mode 100644 index 0000000..7498d86 --- /dev/null +++ b/extensions/twig/yii/twig/ViewRenderer.php @@ -0,0 +1,73 @@ + + * @since 2.0 + */ +class ViewRenderer extends BaseViewRenderer +{ + /** + * @var string the directory or path alias pointing to where Twig cache will be stored. + */ + public $cachePath = '@app/runtime/Twig/cache'; + + /** + * @var array Twig options + * @see http://twig.sensiolabs.org/doc/api.html#environment-options + */ + public $options = array(); + + /** + * @var \Twig_Environment + */ + public $twig; + + public function init() + { + $loader = new \Twig_Loader_String(); + + $this->twig = new \Twig_Environment($loader, array_merge(array( + 'cache' => Yii::getAlias($this->cachePath), + ), $this->options)); + + $this->twig->addFunction('path', new \Twig_Function_Function(function($path, $args = array()){ + return Html::url(array_merge(array($path), $args)); + })); + + $this->twig->addGlobal('app', \Yii::$app); + } + + /** + * 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) + { + $this->twig->addGlobal('this', $view); + return $this->twig->render(file_get_contents($file), $params); + } +} diff --git a/framework/yii/renderers/SmartyViewRenderer.php b/framework/yii/renderers/SmartyViewRenderer.php deleted file mode 100644 index ac66e0d..0000000 --- a/framework/yii/renderers/SmartyViewRenderer.php +++ /dev/null @@ -1,99 +0,0 @@ - - * @since 2.0 - */ -class SmartyViewRenderer extends ViewRenderer -{ - /** - * @var string the directory or path alias pointing to where Smarty cache will be stored. - */ - public $cachePath = '@app/runtime/Smarty/cache'; - - /** - * @var string the directory or path alias pointing to where Smarty compiled templates will be stored. - */ - public $compilePath = '@app/runtime/Smarty/compile'; - - /** - * @var Smarty - */ - public $smarty; - - public function init() - { - $this->smarty = new Smarty(); - $this->smarty->setCompileDir(Yii::getAlias($this->compilePath)); - $this->smarty->setCacheDir(Yii::getAlias($this->cachePath)); - - $this->smarty->registerPlugin('function', 'path', array($this, 'smarty_function_path')); - } - - /** - * Smarty template function to get a path for using in links - * - * Usage is the following: - * - * {path route='blog/view' alias=$post.alias user=$user.id} - * - * where route is Yii route and the rest of parameters are passed as is. - * - * @param $params - * @param \Smarty_Internal_Template $template - * - * @return string - */ - public function smarty_function_path($params, \Smarty_Internal_Template $template) - { - if (!isset($params['route'])) { - trigger_error("path: missing 'route' parameter"); - } - - array_unshift($params, $params['route']) ; - unset($params['route']); - - return Html::url($params); - } - - /** - * 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); - /** @var \Smarty_Internal_Template $template */ - $template = $this->smarty->createTemplate($file, null, null, $params, true); - - $template->assign('app', \Yii::$app); - $template->assign('this', $view); - - return $template->fetch(); - } -} diff --git a/framework/yii/renderers/TwigViewRenderer.php b/framework/yii/renderers/TwigViewRenderer.php deleted file mode 100644 index de561d3..0000000 --- a/framework/yii/renderers/TwigViewRenderer.php +++ /dev/null @@ -1,73 +0,0 @@ - - * @since 2.0 - */ -class TwigViewRenderer extends ViewRenderer -{ - /** - * @var string the directory or path alias pointing to where Twig cache will be stored. - */ - public $cachePath = '@app/runtime/Twig/cache'; - - /** - * @var array Twig options - * @see http://twig.sensiolabs.org/doc/api.html#environment-options - */ - public $options = array(); - - /** - * @var \Twig_Environment - */ - public $twig; - - public function init() - { - $loader = new \Twig_Loader_String(); - - $this->twig = new \Twig_Environment($loader, array_merge(array( - 'cache' => Yii::getAlias($this->cachePath), - ), $this->options)); - - $this->twig->addFunction('path', new \Twig_Function_Function(function($path, $args = array()){ - return Html::url(array_merge(array($path), $args)); - })); - - $this->twig->addGlobal('app', \Yii::$app); - } - - /** - * 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) - { - $this->twig->addGlobal('this', $view); - return $this->twig->render(file_get_contents($file), $params); - } -}