mirror of
https://github.com/Mibew/handlebars.php.git
synced 2024-11-15 08:44:12 +03:00
Added custom template class option
This commit is contained in:
parent
b3fcfe1339
commit
69d4efe259
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -45,9 +45,14 @@ class Template
|
||||
*/
|
||||
protected $handlebars;
|
||||
|
||||
|
||||
/**
|
||||
* @var array The tokenized tree
|
||||
*/
|
||||
protected $tree = array();
|
||||
|
||||
/**
|
||||
* @var string The template source
|
||||
*/
|
||||
protected $source = '';
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
**/
|
||||
|
12
tests/fixture/Handlebars/CustomTemplate.php
Normal file
12
tests/fixture/Handlebars/CustomTemplate.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Handlebars;
|
||||
|
||||
use Handlebars\Template;
|
||||
|
||||
class CustomTemplate extends Template
|
||||
{
|
||||
public function render($context)
|
||||
{
|
||||
return 'Altered Template';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user