From 69d4efe2598b21fce0e8995764a82b34dcf8b838 Mon Sep 17 00:00:00 2001 From: Jeff Turcotte Date: Mon, 15 Dec 2014 19:37:13 -0500 Subject: [PATCH] Added custom template class option --- src/Handlebars/Handlebars.php | 37 ++++++++++++++++++--- src/Handlebars/Template.php | 7 +++- tests/Xamin/HandlebarsTest.php | 33 ++++++++++++++++++ tests/fixture/Handlebars/CustomTemplate.php | 12 +++++++ 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 tests/fixture/Handlebars/CustomTemplate.php diff --git a/src/Handlebars/Handlebars.php b/src/Handlebars/Handlebars.php index 0057d4c..5f4462c 100755 --- a/src/Handlebars/Handlebars.php +++ b/src/Handlebars/Handlebars.php @@ -9,6 +9,7 @@ * @package Handlebars * @author fzerorubigd * @author Behrooz Shabani + * @author Jeff Turcotte * @copyright 2010-2012 (c) Justin Hileman * @copyright 2012 (c) ParsPooyesh Co * @copyright 2013 (c) Behrooz Shabani @@ -84,6 +85,11 @@ class Handlebars */ private $_cache; + /** + * @var string the class to use for the template + */ + private $_templateClass = 'Handlebars\\Template'; + /** * @var callable escape function to use */ @@ -108,6 +114,7 @@ class Handlebars * loader => Loader object * partials_loader => Loader object * cache => Cache object + * template_class => the class to use for the template object * * @param array $options array of options to set * @@ -131,6 +138,10 @@ class Handlebars $this->setCache($options['cache']); } + if (isset($options['template_class'])) { + $this->setTemplateClass($options['template_class']); + } + if (isset($options['escape'])) { if (!is_callable($options['escape'])) { throw new \InvalidArgumentException( @@ -438,6 +449,24 @@ class Handlebars return $this->_parser; } + /** + * Sets the class to use for the template object + * + * @param string $class the class name + * + * @return void + */ + public function setTemplateClass($class) + { + if (!is_a($class, 'Handlebars\\Template', true)) { + throw new \InvalidArgumentException( + 'Custom template class must extend Template' + ); + } + + $this->_templateClass = $class; + } + /** * Load a template by name with current template loader * @@ -450,7 +479,7 @@ class Handlebars $source = $this->getLoader()->load($name); $tree = $this->_tokenize($source); - return new Template($this, $tree, $source); + return new $this->_templateClass($this, $tree, $source); } /** @@ -468,7 +497,7 @@ class Handlebars $source = $this->getPartialsLoader()->load($name); $tree = $this->_tokenize($source); - return new Template($this, $tree, $source); + return new $this->_templateClass($this, $tree, $source); } /** @@ -509,7 +538,7 @@ class Handlebars { $tree = $this->_tokenize($source); - return new Template($this, $tree, $source); + return new $this->_templateClass($this, $tree, $source); } /** @@ -532,4 +561,4 @@ class Handlebars return $tree; } -} \ No newline at end of file +} diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 1cfe4f4..45bdd37 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -45,9 +45,14 @@ class Template */ protected $handlebars; - + /** + * @var array The tokenized tree + */ protected $tree = array(); + /** + * @var string The template source + */ protected $source = ''; /** diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 58f60cb..9fa5d5b 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -780,6 +780,39 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase ); } + + /** + * Test invalid custom template class + * + * @expectedException \InvalidArgumentException + */ + public function testInvalidCustomTemplateClass() + { + $loader = new \Handlebars\Loader\StringLoader(); + $engine = new \Handlebars\Handlebars(array( + 'loader' => $loader, + 'template_class' => 'stdclass' + )); + } + + /** + * Test custom template class + */ + public function testValidCustomTemplateClass() + { + Handlebars\Autoloader::register(realpath(__DIR__ . '/../fixture/')); + + $loader = new \Handlebars\Loader\StringLoader(); + $engine = new \Handlebars\Handlebars(array( + 'loader' => $loader, + 'template_class' => 'Handlebars\CustomTemplate' + )); + + $render = $engine->render('Original Template', array()); + + $this->assertEquals($render, 'Altered Template'); + } + /** * Test for proper handling of the length property **/ diff --git a/tests/fixture/Handlebars/CustomTemplate.php b/tests/fixture/Handlebars/CustomTemplate.php new file mode 100644 index 0000000..b13b706 --- /dev/null +++ b/tests/fixture/Handlebars/CustomTemplate.php @@ -0,0 +1,12 @@ +