4 changed files with 247 additions and 0 deletions
			
			
		@ -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. | 
				
			||||||
@ -0,0 +1,56 @@ | 
				
			|||||||
 | 
					<?php | 
				
			||||||
 | 
					/** | 
				
			||||||
 | 
					 * Composite view renderer class file. | 
				
			||||||
 | 
					 * | 
				
			||||||
 | 
					 * @link http://www.yiiframework.com/ | 
				
			||||||
 | 
					 * @copyright Copyright © 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); | 
				
			||||||
 | 
							} | 
				
			||||||
 | 
						} | 
				
			||||||
 | 
					} | 
				
			||||||
@ -0,0 +1,78 @@ | 
				
			|||||||
 | 
					<?php | 
				
			||||||
 | 
					/** | 
				
			||||||
 | 
					 * Smarty view renderer class file. | 
				
			||||||
 | 
					 * | 
				
			||||||
 | 
					 * @link http://www.yiiframework.com/ | 
				
			||||||
 | 
					 * @copyright Copyright © 2008 Yii Software LLC | 
				
			||||||
 | 
					 * @license http://www.yiiframework.com/license/ | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace yii\renderers; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use \yii\base\View; | 
				
			||||||
 | 
					use \yii\base\ViewRenderer; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** | 
				
			||||||
 | 
					 * SmartyViewRenderer allows you to use Smarty templates in views. | 
				
			||||||
 | 
					 * | 
				
			||||||
 | 
					 * @author Alexander Makarov <sam@rmcreative.ru> | 
				
			||||||
 | 
					 * @since 2.0 | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					class SmartyViewRenderer extends ViewRenderer | 
				
			||||||
 | 
					{ | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * @var string alias pointing to where Smarty code is located. | 
				
			||||||
 | 
						 */ | 
				
			||||||
 | 
						public $smartyDir = '@app/vendors/Smarty'; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * @var string alias pointing to where Smarty cache will be stored. | 
				
			||||||
 | 
						 */ | 
				
			||||||
 | 
						public $cacheDir = '@app/runtime/Smarty/cache'; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * @var string alias pointing to where Smarty compiled teamplates will be stored. | 
				
			||||||
 | 
						 */ | 
				
			||||||
 | 
						public $compileDir = '@app/runtime/Smarty/compile'; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * @var string file extension to use for template files | 
				
			||||||
 | 
						 */ | 
				
			||||||
 | 
						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); | 
				
			||||||
 | 
							} | 
				
			||||||
 | 
						} | 
				
			||||||
 | 
					} | 
				
			||||||
@ -0,0 +1,82 @@ | 
				
			|||||||
 | 
					<?php | 
				
			||||||
 | 
					/** | 
				
			||||||
 | 
					 * Twig view renderer class file. | 
				
			||||||
 | 
					 * | 
				
			||||||
 | 
					 * @link http://www.yiiframework.com/ | 
				
			||||||
 | 
					 * @copyright Copyright © 2008 Yii Software LLC | 
				
			||||||
 | 
					 * @license http://www.yiiframework.com/license/ | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace yii\renderers; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use \yii\base\View; | 
				
			||||||
 | 
					use \yii\base\ViewRenderer; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** | 
				
			||||||
 | 
					 * TwigViewRenderer allows you to use Twig templates in views. | 
				
			||||||
 | 
					 * | 
				
			||||||
 | 
					 * @author Alexander Makarov <sam@rmcreative.ru> | 
				
			||||||
 | 
					 * @since 2.0 | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					class TwigViewRenderer extends ViewRenderer | 
				
			||||||
 | 
					{ | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * @var string alias pointing to where Twig code is located. | 
				
			||||||
 | 
						 */ | 
				
			||||||
 | 
						public $twigDir = '@app/vendors/Twig'; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * @var string 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'; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * @var array Twig options | 
				
			||||||
 | 
						 * @see http://twig.sensiolabs.org/doc/api.html#environment-options | 
				
			||||||
 | 
						 */ | 
				
			||||||
 | 
						public $options = array(); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * @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_merge(array( | 
				
			||||||
 | 
								'cache' => \Yii::getAlias($this->cacheDir), | 
				
			||||||
 | 
							), $this->options)); | 
				
			||||||
 | 
						} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/** | 
				
			||||||
 | 
						 * 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); | 
				
			||||||
 | 
							} | 
				
			||||||
 | 
						} | 
				
			||||||
 | 
					} | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue