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
12 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/
|
||
|
*/
|
||
|
|
||
|
namespace yii\renderers;
|
||
|
|
||
12 years ago
|
use Yii;
|
||
|
use yii\base\View;
|
||
12 years ago
|
use yii\base\ViewRenderer as BaseViewRenderer;
|
||
12 years ago
|
use yii\helpers\Html;
|
||
12 years ago
|
|
||
|
/**
|
||
|
* TwigViewRenderer allows you to use Twig templates in views.
|
||
|
*
|
||
|
* @author Alexander Makarov <sam@rmcreative.ru>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class ViewRenderer extends BaseViewRenderer
|
||
12 years ago
|
{
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var string the directory or path alias pointing to where Twig cache will be stored.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public $cachePath = '@app/runtime/Twig/cache';
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* @var array Twig options
|
||
12 years ago
|
* @see http://twig.sensiolabs.org/doc/api.html#environment-options
|
||
|
*/
|
||
|
public $options = array();
|
||
|
|
||
|
/**
|
||
12 years ago
|
* @var \Twig_Environment
|
||
|
*/
|
||
12 years ago
|
public $twig;
|
||
12 years ago
|
|
||
|
public function init()
|
||
|
{
|
||
|
$loader = new \Twig_Loader_String();
|
||
12 years ago
|
|
||
12 years ago
|
$this->twig = new \Twig_Environment($loader, array_merge(array(
|
||
|
'cache' => Yii::getAlias($this->cachePath),
|
||
12 years ago
|
), $this->options));
|
||
12 years ago
|
|
||
|
$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);
|
||
12 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)
|
||
|
{
|
||
12 years ago
|
$this->twig->addGlobal('this', $view);
|
||
12 years ago
|
return $this->twig->render(file_get_contents($file), $params);
|
||
12 years ago
|
}
|
||
|
}
|