mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-05-03 10:33:08 +03:00
Merge pull request #102 from jeffturcotte/master
Added custom template class option
This commit is contained in:
commit
d0bbcb07b7
@ -9,6 +9,7 @@
|
|||||||
* @package Handlebars
|
* @package Handlebars
|
||||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||||
* @author Behrooz Shabani <everplays@gmail.com>
|
* @author Behrooz Shabani <everplays@gmail.com>
|
||||||
|
* @author Jeff Turcotte <jeff.turcotte@gmail.com>
|
||||||
* @copyright 2010-2012 (c) Justin Hileman
|
* @copyright 2010-2012 (c) Justin Hileman
|
||||||
* @copyright 2012 (c) ParsPooyesh Co
|
* @copyright 2012 (c) ParsPooyesh Co
|
||||||
* @copyright 2013 (c) Behrooz Shabani
|
* @copyright 2013 (c) Behrooz Shabani
|
||||||
@ -84,6 +85,11 @@ class Handlebars
|
|||||||
*/
|
*/
|
||||||
private $_cache;
|
private $_cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string the class to use for the template
|
||||||
|
*/
|
||||||
|
private $_templateClass = 'Handlebars\\Template';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var callable escape function to use
|
* @var callable escape function to use
|
||||||
*/
|
*/
|
||||||
@ -108,6 +114,7 @@ class Handlebars
|
|||||||
* loader => Loader object
|
* loader => Loader object
|
||||||
* partials_loader => Loader object
|
* partials_loader => Loader object
|
||||||
* cache => Cache object
|
* cache => Cache object
|
||||||
|
* template_class => the class to use for the template object
|
||||||
*
|
*
|
||||||
* @param array $options array of options to set
|
* @param array $options array of options to set
|
||||||
*
|
*
|
||||||
@ -131,6 +138,10 @@ class Handlebars
|
|||||||
$this->setCache($options['cache']);
|
$this->setCache($options['cache']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($options['template_class'])) {
|
||||||
|
$this->setTemplateClass($options['template_class']);
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($options['escape'])) {
|
if (isset($options['escape'])) {
|
||||||
if (!is_callable($options['escape'])) {
|
if (!is_callable($options['escape'])) {
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
@ -438,6 +449,24 @@ class Handlebars
|
|||||||
return $this->_parser;
|
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
|
* Load a template by name with current template loader
|
||||||
*
|
*
|
||||||
@ -450,7 +479,7 @@ class Handlebars
|
|||||||
$source = $this->getLoader()->load($name);
|
$source = $this->getLoader()->load($name);
|
||||||
$tree = $this->_tokenize($source);
|
$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);
|
$source = $this->getPartialsLoader()->load($name);
|
||||||
$tree = $this->_tokenize($source);
|
$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);
|
$tree = $this->_tokenize($source);
|
||||||
|
|
||||||
return new Template($this, $tree, $source);
|
return new $this->_templateClass($this, $tree, $source);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,9 +45,14 @@ class Template
|
|||||||
*/
|
*/
|
||||||
protected $handlebars;
|
protected $handlebars;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array The tokenized tree
|
||||||
|
*/
|
||||||
protected $tree = array();
|
protected $tree = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string The template source
|
||||||
|
*/
|
||||||
protected $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
|
* 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