* @since 2.0 */ class TwigViewRenderer extends ViewRenderer { public $twigDir = '@app/vendors/Twig'; public $cacheDir = '@app/runtime/Twig/cache'; public $fileExtension = 'twig'; /** * @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( 'cache' => \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) { return $this->_twig->render(file_get_contents($file), $params); } else { return $view->renderPhpFile($file, $params); } } }