Skip to content

Commit

Permalink
add support for inline helper without #
Browse files Browse the repository at this point in the history
  • Loading branch information
fzerorubigd committed Dec 6, 2013
1 parent 000bdd6 commit 82ea9f9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Handlebars/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public function render($context)
$buffer .= $this->_variables($context, $current, false);
break;
case Tokenizer::T_ESCAPED:

$buffer .= $this->_variables($context, $current, true);
break;
case Tokenizer::T_TEXT:
Expand Down Expand Up @@ -340,7 +341,26 @@ private function _partial(Context $context, $current)
}

/**
* 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 array $current section node data
Expand All @@ -350,6 +370,13 @@ private function _partial(Context $context, $current)
*/
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];
$value = $context->get($name);
if ($name == '@index') {
Expand Down
8 changes: 8 additions & 0 deletions tests/Xamin/HandlebarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public function testCustomHelper()
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()));

$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()));

}

/**
Expand Down

0 comments on commit 82ea9f9

Please sign in to comment.