Added custom template class option

This commit is contained in:
Jeff Turcotte 2014-12-15 19:37:13 -05:00
parent b3fcfe1339
commit 69d4efe259
4 changed files with 84 additions and 5 deletions

View File

@ -9,6 +9,7 @@
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @author Jeff Turcotte <jeff.turcotte@gmail.com>
* @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;
}
}
}

View File

@ -45,9 +45,14 @@ class Template
*/
protected $handlebars;
/**
* @var array The tokenized tree
*/
protected $tree = array();
/**
* @var string The template source
*/
protected $source = '';
/**

View File

@ -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
**/

View File

@ -0,0 +1,12 @@
<?php
namespace Handlebars;
use Handlebars\Template;
class CustomTemplate extends Template
{
public function render($context)
{
return 'Altered Template';
}
}