mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-05-03 18:43:07 +03:00
change default helpers to anonymus function again
This commit is contained in:
parent
f13c6a3cef
commit
000bdd6e74
@ -85,7 +85,11 @@ class Autoloader
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$file = sprintf('%s/%s.php', $this->_baseDir, str_replace('\\', '/', $class));
|
$file = sprintf(
|
||||||
|
'%s/%s.php',
|
||||||
|
$this->_baseDir,
|
||||||
|
str_replace('\\', '/', $class)
|
||||||
|
);
|
||||||
|
|
||||||
if (is_file($file)) {
|
if (is_file($file)) {
|
||||||
include $file;
|
include $file;
|
||||||
|
@ -47,7 +47,8 @@ class Helpers
|
|||||||
* Create new helper container class
|
* Create new helper container class
|
||||||
*
|
*
|
||||||
* @param array $helpers array of name=>$value helpers
|
* @param array $helpers array of name=>$value helpers
|
||||||
* @param array|bool $defaults add defaults helper (if, unless, each,with, bindAttr)
|
* @param array|bool $defaults add defaults helper
|
||||||
|
* (if, unless, each,with, bindAttr)
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException when $helpers is not an array
|
* @throws \InvalidArgumentException when $helpers is not an array
|
||||||
* (or traversable) or helper is not a callable
|
* (or traversable) or helper is not a callable
|
||||||
@ -69,22 +70,23 @@ class Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create handler for the 'if' helper.
|
* Add default helpers (if unless each with bindAttr)
|
||||||
*
|
*
|
||||||
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
|
* @return void
|
||||||
* functions.
|
|
||||||
*
|
|
||||||
* @param \Handlebars\Template $template template that is being rendered
|
|
||||||
* @param \Handlebars\Context $context context object
|
|
||||||
* @param array $args passed arguments to helper
|
|
||||||
* @param string $source part of template that is wrapped
|
|
||||||
* within helper
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public static function helperIf($template, $context, $args, $source)
|
protected function addDefaultHelpers()
|
||||||
{
|
{
|
||||||
|
$this->add(
|
||||||
|
'if',
|
||||||
|
function ($template, $context, $args, $source) {
|
||||||
|
/**
|
||||||
|
* @var $template \Handlebars\Template
|
||||||
|
* @var $context \Handlebars\Context
|
||||||
|
* @var $args array
|
||||||
|
* @var $source string
|
||||||
|
*/
|
||||||
$tmp = $context->get($args);
|
$tmp = $context->get($args);
|
||||||
|
|
||||||
if ($tmp) {
|
if ($tmp) {
|
||||||
@ -101,23 +103,17 @@ class Helpers
|
|||||||
|
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->add(
|
||||||
|
'each',
|
||||||
|
function ($template, $context, $args, $source) {
|
||||||
/**
|
/**
|
||||||
* Create handler for the 'each' helper.
|
* @var $template \Handlebars\Template
|
||||||
*
|
* @var $context \Handlebars\Context
|
||||||
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
|
* @var $args array
|
||||||
* functions.
|
* @var $source string
|
||||||
*
|
|
||||||
* @param \Handlebars\Template $template template that is being rendered
|
|
||||||
* @param \Handlebars\Context $context context object
|
|
||||||
* @param array $args passed arguments to helper
|
|
||||||
* @param string $source part of template that is wrapped
|
|
||||||
* within helper
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public static function helperEach($template, $context, $args, $source)
|
|
||||||
{
|
|
||||||
$tmp = $context->get($args);
|
$tmp = $context->get($args);
|
||||||
$buffer = '';
|
$buffer = '';
|
||||||
if (is_array($tmp) || $tmp instanceof \Traversable) {
|
if (is_array($tmp) || $tmp instanceof \Traversable) {
|
||||||
@ -142,22 +138,17 @@ class Helpers
|
|||||||
|
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->add(
|
||||||
|
'unless',
|
||||||
|
function ($template, $context, $args, $source) {
|
||||||
/**
|
/**
|
||||||
* Create handler for the 'unless' helper.
|
* @var $template \Handlebars\Template
|
||||||
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
|
* @var $context \Handlebars\Context
|
||||||
* functions.
|
* @var $args array
|
||||||
*
|
* @var $source string
|
||||||
* @param \Handlebars\Template $template template that is being rendered
|
|
||||||
* @param \Handlebars\Context $context context object
|
|
||||||
* @param array $args passed arguments to helper
|
|
||||||
* @param string $source part of template that is wrapped
|
|
||||||
* within helper
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public static function helperUnless($template, $context, $args, $source)
|
|
||||||
{
|
|
||||||
$tmp = $context->get($args);
|
$tmp = $context->get($args);
|
||||||
$buffer = '';
|
$buffer = '';
|
||||||
if (!$tmp) {
|
if (!$tmp) {
|
||||||
@ -166,22 +157,17 @@ class Helpers
|
|||||||
|
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->add(
|
||||||
|
'with',
|
||||||
|
function ($template, $context, $args, $source) {
|
||||||
/**
|
/**
|
||||||
* Create handler for the 'with' helper.
|
* @var $template \Handlebars\Template
|
||||||
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
|
* @var $context \Handlebars\Context
|
||||||
* functions.
|
* @var $args array
|
||||||
*
|
* @var $source string
|
||||||
* @param \Handlebars\Template $template template that is being rendered
|
|
||||||
* @param \Handlebars\Context $context context object
|
|
||||||
* @param array $args passed arguments to helper
|
|
||||||
* @param string $source part of template that is wrapped
|
|
||||||
* within helper
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public static function helperWith($template, $context, $args, $source)
|
|
||||||
{
|
|
||||||
$tmp = $context->get($args);
|
$tmp = $context->get($args);
|
||||||
$context->push($tmp);
|
$context->push($tmp);
|
||||||
$buffer = $template->render($context);
|
$buffer = $template->render($context);
|
||||||
@ -189,56 +175,20 @@ class Helpers
|
|||||||
|
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create handler for the 'bindAttr' helper.
|
|
||||||
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
|
|
||||||
* functions.
|
|
||||||
*
|
|
||||||
* @param \Handlebars\Template $template template that is being rendered
|
|
||||||
* @param \Handlebars\Context $context context object
|
|
||||||
* @param array $args passed arguments to helper
|
|
||||||
* @param string $source part of template that is wrapped
|
|
||||||
* within helper
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function helperBindAttr($template, $context, $args, $source)
|
|
||||||
{
|
|
||||||
return $args;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add default helpers (if unless each with bindAttr)
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
protected function addDefaultHelpers()
|
|
||||||
{
|
|
||||||
$this->add(
|
|
||||||
'if',
|
|
||||||
array('Handlebars\Helpers', 'helperIf')
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->add(
|
|
||||||
'each',
|
|
||||||
array('Handlebars\Helpers', 'helperEach')
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->add(
|
|
||||||
'unless',
|
|
||||||
array('Handlebars\Helpers', 'helperUnless')
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->add(
|
|
||||||
'with',
|
|
||||||
array('Handlebars\Helpers', 'helperWith')
|
|
||||||
);
|
);
|
||||||
|
|
||||||
//Just for compatibility with ember
|
//Just for compatibility with ember
|
||||||
$this->add(
|
$this->add(
|
||||||
'bindAttr',
|
'bindAttr',
|
||||||
array('Handlebars\Helpers', 'helperBindAttr')
|
function ($template, $context, $args, $source) {
|
||||||
|
/**
|
||||||
|
* @var $template \Handlebars\Template
|
||||||
|
* @var $context \Handlebars\Context
|
||||||
|
* @var $args array
|
||||||
|
* @var $source string
|
||||||
|
*/
|
||||||
|
return $args;
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,15 +200,10 @@ class Template
|
|||||||
/**
|
/**
|
||||||
* Discard top tree
|
* Discard top tree
|
||||||
*
|
*
|
||||||
* @param mixed $context current context
|
|
||||||
*
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function discard($context)
|
public function discard()
|
||||||
{
|
{
|
||||||
if (!$context instanceof Context) {
|
|
||||||
$context = new Context($context);
|
|
||||||
}
|
|
||||||
$topTree = end($this->_stack); //This method never pop a value from stack
|
$topTree = end($this->_stack); //This method never pop a value from stack
|
||||||
list($index, $tree, $stop) = $topTree;
|
list($index, $tree, $stop) = $topTree;
|
||||||
while (array_key_exists($index, $tree)) {
|
while (array_key_exists($index, $tree)) {
|
||||||
|
@ -111,6 +111,17 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('data' => array(1, 2, 3, 4)),
|
array('data' => array(1, 2, 3, 4)),
|
||||||
'1234'
|
'1234'
|
||||||
),
|
),
|
||||||
|
array(
|
||||||
|
'{{#unless data}}ok{{/unless}}',
|
||||||
|
array('data' => true),
|
||||||
|
''
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{{#unless data}}ok{{/unless}}',
|
||||||
|
array('data' => false),
|
||||||
|
'ok'
|
||||||
|
)
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,7 +214,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testVariableAccess()
|
public function testVariableAccess()
|
||||||
{
|
{
|
||||||
$loader = new \Handlebars\Loader\StringLoader();
|
$loader = new \Handlebars\Loader\StringLoader();
|
||||||
$engine = new \Handlebars\Handlebars();
|
$engine = \Handlebars\Handlebars::factory();
|
||||||
$engine->setLoader($loader);
|
$engine->setLoader($loader);
|
||||||
|
|
||||||
$var = new \StdClass();
|
$var = new \StdClass();
|
||||||
|
Loading…
Reference in New Issue
Block a user