mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-05-03 02:26:41 +03:00
parent
000bdd6e74
commit
82ea9f958d
@ -175,6 +175,7 @@ class Template
|
|||||||
$buffer .= $this->_variables($context, $current, false);
|
$buffer .= $this->_variables($context, $current, false);
|
||||||
break;
|
break;
|
||||||
case Tokenizer::T_ESCAPED:
|
case Tokenizer::T_ESCAPED:
|
||||||
|
|
||||||
$buffer .= $this->_variables($context, $current, true);
|
$buffer .= $this->_variables($context, $current, true);
|
||||||
break;
|
break;
|
||||||
case Tokenizer::T_TEXT:
|
case Tokenizer::T_TEXT:
|
||||||
@ -340,7 +341,26 @@ class Template
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process partial section
|
* Check if there is a helper with this variable name available or not.
|
||||||
|
* if there is a helper registered with this name,
|
||||||
|
* then call that section and ignore it as a variable
|
||||||
|
* for special cases like {{helper arg}} instead of {{#helper arg}}
|
||||||
|
*
|
||||||
|
* @param array $current current token
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
private function _isSection($current)
|
||||||
|
{
|
||||||
|
$helpers = $this->getEngine()->getHelpers();
|
||||||
|
// Tokenizer do not process the args -if any- so be aware of that
|
||||||
|
$name = explode(' ', $current[Tokenizer::NAME]);
|
||||||
|
return $helpers->has(reset($name));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process variables
|
||||||
*
|
*
|
||||||
* @param Context $context current context
|
* @param Context $context current context
|
||||||
* @param array $current section node data
|
* @param array $current section node data
|
||||||
@ -350,6 +370,13 @@ class Template
|
|||||||
*/
|
*/
|
||||||
private function _variables(Context $context, $current, $escaped)
|
private function _variables(Context $context, $current, $escaped)
|
||||||
{
|
{
|
||||||
|
if ($this->_isSection($current)) {
|
||||||
|
$args = explode(' ', $current[Tokenizer::NAME]);
|
||||||
|
$name = array_shift($args);
|
||||||
|
$current[Tokenizer::NAME] = $name;
|
||||||
|
$current[Tokenizer::ARGS] = implode(' ', $args);
|
||||||
|
return $this->_section($context, $current);
|
||||||
|
}
|
||||||
$name = $current[Tokenizer::NAME];
|
$name = $current[Tokenizer::NAME];
|
||||||
$value = $context->get($name);
|
$value = $context->get($name);
|
||||||
if ($name == '@index') {
|
if ($name == '@index') {
|
||||||
|
@ -150,6 +150,14 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
return 'Test helper is called';
|
return 'Test helper is called';
|
||||||
});
|
});
|
||||||
$this->assertEquals('Test helper is called', $engine->render('{{#test}}', array()));
|
$this->assertEquals('Test helper is called', $engine->render('{{#test}}', array()));
|
||||||
|
$this->assertEquals('Test helper is called', $engine->render('{{test}}', array()));
|
||||||
|
|
||||||
|
$engine->addHelper('test2', function ($template, $context, $arg) {
|
||||||
|
return 'Test helper is called with ' . $arg;
|
||||||
|
});
|
||||||
|
$this->assertEquals('Test helper is called with a b c', $engine->render('{{#test2 a b c}}', array()));
|
||||||
|
$this->assertEquals('Test helper is called with a b c', $engine->render('{{test2 a b c}}', array()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user