Commit: fbc5aaafa8a90a1ffab06eb65075a4cd4614ebfc

Author: Joël Perras | Date: 2010-03-03 00:26:33 -0500
Refactoring Twig adapter to use a plugin bootstrap.
diff --git a/config/bootstrap.php b/config/bootstrap.php new file mode 100644 index 0000000..4d03771 --- /dev/null +++ b/config/bootstrap.php @@ -0,0 +1,20 @@ +<?php + +use \lithium\core\Libraries; + +/** + * This is the path to the li3_twig plugin, used for Libraries path resolution. + */ +define('LI3_TWIG_PATH', dirname(__DIR__)); + + +/** + * Add the Twig libraries + */ +Libraries::add('Twig', array( + 'path' => LI3_TWIG_PATH . '/libraries/Twig/lib/Twig', + 'prefix' => 'Twig_', + 'loader' => 'Twig_Autoloader::autoload', +)); + +?> \ No newline at end of file diff --git a/template/view/adapter/Twig.php b/template/view/adapter/Twig.php index 6119f75..3e0afcc 100644 --- a/template/view/adapter/Twig.php +++ b/template/view/adapter/Twig.php @@ -25,29 +25,33 @@ class Twig extends \lithium\template\view\Renderer { * * @var object */ - protected $_environment = null; + public $environment = null; /** - * Sets up the necesarry libraries and autoloaders for this view adapter. + * Constructor for this adapter - sets relevant default configurations for Twig to be used + * when instantiating a new Twig_Environment and Twig_Loader_Filesystem. * - * @param array $config + * @param array $config Optional configuration directives. + * Please see http://www.twig-project.org/book/03-Twig-for-Developers for all + * available configuration keys and their description. * @return void */ public function __construct($config = array()) { - $config += array('cache' => null, 'debug' => true, 'auto_reloader' => true); - parent::__construct($config); - - Libraries::add('Twig', array( - 'path' => realpath(__DIR__ . '/../../../libraries/Twig/lib/Twig'), - 'prefix' => 'Twig_', - 'loader' => 'Twig_Autoloader::autoload', - )); + $defaults = array( + 'cache' => LITHIUM_APP_PATH . '/resources/tmp/cache/templates', + ); + parent::__construct($config + $defaults); + } - $this->_environment = new Twig_Environment(new Twig_Loader_Filesystem(array()), array( - 'cache' => null,//LITHIUM_APP_PATH . '/resources/tmp/cache/templates', - 'debug' => true, - 'auto_reloader' => true, - )); + /** + * Initialize the necessary Twig objects & attach them to the current object instance. + * + * @return void + */ + public function _init() { + parent::_init(); + $Loader = new Twig_Loader_Filesystem(array()); + $this->environment = new Twig_Environment($Loader, $this->_config); } /** @@ -65,10 +69,10 @@ class Twig extends \lithium\template\view\Renderer { return dirname($item); }, $paths); - $this->_environment->getLoader()->setPaths($directories); + $this->environment->getLoader()->setPaths($directories); //Loading template.. Will look in all the paths. - $template = $this->_environment->loadTemplate(basename($paths[0])); + $template = $this->environment->loadTemplate(basename($paths[0])); //Because $this is not availible in the Twig template view is used as a substitute. return $template->render((array) $data + array('this' => $this));