Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Fix handling of @root helper arguments #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Handlebars/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function parse($args_string)
$bad_chars = preg_quote(Context::NOT_VALID_NAME_CHARS, '#');
$bad_seg_chars = preg_quote(Context::NOT_VALID_SEGMENT_NAME_CHARS, '#');

$name_chunk = '(?:[^' . $bad_chars . '\s]+)|(?:\[[^' . $bad_seg_chars . ']+\])';
$name_chunk = '(?:@?[^' . $bad_chars . '\s]+)|(?:\[[^' . $bad_seg_chars . ']+\])';
$variable_name = '(?:\.\.\/)*(?:(?:' . $name_chunk . ')[\.\/])*(?:' . $name_chunk . ')\.?';
$special_variable_name = '@[a-z]+';
$escaped_value = '(?:(?<!\\\\)".*?(?<!\\\\)"|(?<!\\\\)\'.*?(?<!\\\\)\')';
Expand Down
10 changes: 10 additions & 0 deletions tests/Xamin/HandlebarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ public function rootSpecialVariableProvider()
return array(
array('{{foo}} {{ @root.foo }}', array( 'foo' => 'bar' ), "bar bar"),
array('{{@root.foo}} {{#each arr}}{{ @root.foo }}{{/each}}', array( 'foo' => 'bar', 'arr' => array( '1' ) ), "bar bar"),
array('{{add @root.one @root.two}}', array( 'one' => '1', 'two' => '2' ), "3"),
);
}

Expand All @@ -1268,6 +1269,15 @@ public function rootSpecialVariableProvider()
public function testRootSpecialVariableHelpers($template, $data, $results)
{
$engine = new \Handlebars\Handlebars();
$engine->addHelper('add', function ($template, $context, $args) {
$sum = 0;

foreach ($template->parseArguments($args) as $value) {
$sum += intval($context->get($value));
}

return $sum;
});

$res = $engine->render($template, $data);
$this->assertEquals($res, $results);
Expand Down