From 99a6431e8fc8ef57f37331e132907728b9339b9d Mon Sep 17 00:00:00 2001 From: Stanislav Demydiuk <4you4ever@bigmir.net> Date: Tue, 3 Sep 2013 10:46:55 +0300 Subject: [PATCH 1/2] Auto-add the path to views of the current controller to twig loader --- ETwigViewRenderer.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ETwigViewRenderer.php b/ETwigViewRenderer.php index 50078ac..a3e78c1 100644 --- a/ETwigViewRenderer.php +++ b/ETwigViewRenderer.php @@ -82,6 +82,7 @@ function init() } $this->_paths[] = $app->getBasePath(); + $this->_paths[] = $app->getBasePath() . DIRECTORY_SEPARATOR . 'views'; $loader = new Twig_Loader_Filesystem($this->_paths); @@ -138,6 +139,15 @@ function init() */ public function renderFile($context, $sourceFile, $data, $return) { + // add controller view path + if ($context instanceof CController) { + $path = $context->getViewPath(); + if (!in_array($path, $this->_paths) && file_exists($path)) { + $this->_twig->getLoader()->addPath($path); + $this->_paths[] = $path; + } + } + // current controller properties will be accessible as {{ this.property }} $data['this'] = $context; From c1430df995251072f748776ece3c31722589c037 Mon Sep 17 00:00:00 2001 From: Stanislav Demydiuk <4you4ever@bigmir.net> Date: Tue, 3 Sep 2013 12:15:34 +0300 Subject: [PATCH 2/2] Auto-add the path of the current file to twig loader --- ETwigViewRenderer.php | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/ETwigViewRenderer.php b/ETwigViewRenderer.php index a3e78c1..c7d957e 100644 --- a/ETwigViewRenderer.php +++ b/ETwigViewRenderer.php @@ -19,6 +19,10 @@ class ETwigViewRenderer extends CApplicationComponent implements IViewRenderer * @var string Twig template files extension */ public $fileExtension = '.twig'; + /** + * @var bool Auto-add current controller and file path to twig loader + */ + public $autoAddPath = true; /** * @var array Twig environment options * @see http://twig.sensiolabs.org/doc/api.html#environment-options @@ -140,12 +144,13 @@ function init() public function renderFile($context, $sourceFile, $data, $return) { // add controller view path - if ($context instanceof CController) { - $path = $context->getViewPath(); - if (!in_array($path, $this->_paths) && file_exists($path)) { - $this->_twig->getLoader()->addPath($path); - $this->_paths[] = $path; - } + if ($this->autoAddPath && $context instanceof CController) { + $this->_addPath($context->getViewPath()); + } + + // add current file path + if ($this->autoAddPath && file_exists($sourceFile)) { + $this->_addPath(dirname($sourceFile)); } // current controller properties will be accessible as {{ this.property }} @@ -261,6 +266,14 @@ private function _addCustom($classType, $elements) } } } + + private function _addPath($path) + { + if (!in_array($path, $this->_paths) && file_exists($path)) { + $this->_twig->getLoader()->addPath($path); + $this->_paths[] = $path; + } + } } /**