You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					100 lines
				
				2.5 KiB
			
		
		
			
		
	
	
					100 lines
				
				2.5 KiB
			| 
								 
											13 years ago
										 
									 | 
							
								<?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;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								use Yii;
							 | 
						||
| 
								 | 
							
								use Smarty;
							 | 
						||
| 
								 | 
							
								use yii\base\View;
							 | 
						||
| 
								 | 
							
								use yii\base\ViewRenderer;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								use yii\helpers\Html;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * SmartyViewRenderer allows you to use Smarty templates in views.
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @author Alexander Makarov <sam@rmcreative.ru>
							 | 
						||
| 
								 | 
							
								 * @since 2.0
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								class SmartyViewRenderer extends ViewRenderer
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var string the directory or path alias pointing to where Smarty cache will be stored.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public $cachePath = '@app/runtime/Smarty/cache';
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var string the directory or path alias pointing to where Smarty compiled templates will be stored.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public $compilePath = '@app/runtime/Smarty/compile';
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var Smarty
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public $smarty;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									public function init()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$this->smarty = new Smarty();
							 | 
						||
| 
								 | 
							
										$this->smarty->setCompileDir(Yii::getAlias($this->compilePath));
							 | 
						||
| 
								 | 
							
										$this->smarty->setCacheDir(Yii::getAlias($this->cachePath));
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
										$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)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if (!isset($params['route'])) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											trigger_error("path: missing 'route' parameter");
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										array_unshift($params, $params['route']) ;
							 | 
						||
| 
								 | 
							
										unset($params['route']);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										return Html::url($params);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * 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);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										/** @var \Smarty_Internal_Template $template */
							 | 
						||
| 
								 | 
							
										$template = $this->smarty->createTemplate($file, null, null, $params, true);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
										$template->assign('app', \Yii::$app);
							 | 
						||
| 
								 | 
							
										$template->assign('this', $view);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return $template->fetch();
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								}
							 |