Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ETwigViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,6 +86,7 @@ function init()
}

$this->_paths[] = $app->getBasePath();
$this->_paths[] = $app->getBasePath() . DIRECTORY_SEPARATOR . 'views';

$loader = new Twig_Loader_Filesystem($this->_paths);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
}

/**
Expand Down