diff --git a/ETwigViewRenderer.php b/ETwigViewRenderer.php index 50078ac..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 @@ -82,6 +86,7 @@ function init() } $this->_paths[] = $app->getBasePath(); + $this->_paths[] = $app->getBasePath() . DIRECTORY_SEPARATOR . 'views'; $loader = new Twig_Loader_Filesystem($this->_paths); @@ -138,6 +143,16 @@ function init() */ public function renderFile($context, $sourceFile, $data, $return) { + // add controller view 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 }} $data['this'] = $context; @@ -251,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; + } + } } /**