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.
		
		
		
		
			
				
					74 lines
				
				1.8 KiB
			
		
		
			
		
	
	
					74 lines
				
				1.8 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * Twig view renderer class file.
 | ||
|  |  *
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright © 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											12 years ago
										 | namespace yii\twig;
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | use Yii;
 | ||
|  | use yii\base\View;
 | ||
| 
											13 years ago
										 | use yii\base\ViewRenderer as BaseViewRenderer;
 | ||
| 
											13 years ago
										 | use yii\helpers\Html;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | /**
 | ||
|  |  * TwigViewRenderer allows you to use Twig templates in views.
 | ||
|  |  *
 | ||
|  |  * @author Alexander Makarov <sam@rmcreative.ru>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | class ViewRenderer extends BaseViewRenderer
 | ||
| 
											13 years ago
										 | {
 | ||
| 
											13 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the directory or path alias pointing to where Twig cache will be stored.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $cachePath = '@runtime/Twig/cache';
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array Twig options
 | ||
| 
											13 years ago
										 | 	 * @see http://twig.sensiolabs.org/doc/api.html#environment-options
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $options = [];
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var \Twig_Environment
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public $twig;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	public function init()
 | ||
|  | 	{
 | ||
|  | 		$loader = new \Twig_Loader_String();
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											12 years ago
										 | 		$this->twig = new \Twig_Environment($loader, array_merge([
 | ||
| 
											13 years ago
										 | 			'cache' => Yii::getAlias($this->cachePath),
 | ||
| 
											12 years ago
										 | 		], $this->options));
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											12 years ago
										 | 		$this->twig->addFunction('path', new \Twig_Function_Function(function ($path, $args = []) {
 | ||
|  | 			return Html::url(array_merge([$path], $args));
 | ||
| 
											13 years ago
										 | 		}));
 | ||
|  | 
 | ||
|  | 		$this->twig->addGlobal('app', \Yii::$app);
 | ||
| 
											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)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$this->twig->addGlobal('this', $view);
 | ||
| 
											13 years ago
										 | 		return $this->twig->render(file_get_contents($file), $params);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | }
 |