Make subexpressions play nice with context

This commit is contained in:
Dmitriy Simushev 2014-09-15 09:19:21 +00:00
parent 3bb6dc09a4
commit e863d7af80
2 changed files with 17 additions and 15 deletions

View File

@ -354,9 +354,9 @@ class Template
Tokenizer::ARGS => implode(" ", array_slice($cmd, 1)) Tokenizer::ARGS => implode(" ", array_slice($cmd, 1))
); );
// resolve the node recursively // resolve the node recursively
$resolved = $this->_handlebarsStyleSection($context, $section_node); $resolved = addcslashes($this->_handlebarsStyleSection($context, $section_node), '"');
// replace original subexpression with result // replace original subexpression with result
$current[Tokenizer::ARGS] = str_replace('('.$expr.')', $resolved, $current[Tokenizer::ARGS]); $current[Tokenizer::ARGS] = str_replace('('.$expr.')', '"' . $resolved . '"', $current[Tokenizer::ARGS]);
} }
} }

View File

@ -1049,35 +1049,37 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
{ {
$loader = new \Handlebars\Loader\StringLoader(); $loader = new \Handlebars\Loader\StringLoader();
$engine = new \Handlebars\Handlebars(array('loader' => $loader)); $engine = new \Handlebars\Handlebars(array('loader' => $loader));
$engine->addHelper('test', function ($template, $context, $arg) { $engine->addHelper('test', function ($template, $context, $args) {
return $arg . 'Test.'; $parsedArgs = $template->parseArguments($args);
return (count($parsedArgs) ? $context->get($parsedArgs[0]) : '') . 'Test.';
}); });
// assert that nested syntax is accepted and sub-helper is run // assert that nested syntax is accepted and sub-helper is run
$this->assertEquals('Test.Test.', $engine->render('{{test (test)}}', array())); $this->assertEquals('Test.Test.', $engine->render('{{test (test)}}', array()));
$engine->addHelper('add', function ($template, $context, $arg) { $engine->addHelper('add', function ($template, $context, $args) {
$values = explode(" ", $arg); $sum = 0;
return $values[0] + $values[1]; foreach ($template->parseArguments($args) as $value) {
$sum += intval($context->get($value));
}
return $sum;
}); });
// assert that subexpression result is inserted correctly as argument to top level helper // assert that subexpression result is inserted correctly as argument to top level helper
$this->assertEquals('42', $engine->render('{{add 21 (add 10 (add 5 6))}}', array())); $this->assertEquals('42', $engine->render('{{add 21 (add 10 (add 5 6))}}', array()));
// assert that bracketed expressions within string literals are treated correctly // assert that bracketed expressions within string literals are treated correctly
$this->assertEquals("'(test)'Test.", $engine->render("{{test '(test)'}}", array())); $this->assertEquals("(test)Test.", $engine->render("{{test '(test)'}}", array()));
$this->assertEquals("')'Test.Test.", $engine->render("{{test (test ')')}}", array())); $this->assertEquals(")Test.Test.", $engine->render("{{test (test ')')}}", array()));
$engine->addHelper('concat', function(\Handlebars\Template $template,\Handlebars\Context $context, $args) { $engine->addHelper('concat', function(\Handlebars\Template $template,\Handlebars\Context $context, $args) {
$result = ''; $result = '';
foreach($template->parseArguments($args) as $arg) { foreach($template->parseArguments($args) as $arg) {
if (($trnaslated = $context->get($arg)) !== null ) { $result .= $context->get($arg);
$result .= $trnaslated;
} else {
$result .= $arg;
}
} }
return $result; return $result;
@ -1086,7 +1088,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('ACB', $engine->render('{{concat a (concat c b)}}', array('a' => 'A', 'b' => 'B', 'c' => 'C'))); $this->assertEquals('ACB', $engine->render('{{concat a (concat c b)}}', array('a' => 'A', 'b' => 'B', 'c' => 'C')));
$this->assertEquals('ACB', $engine->render('{{concat (concat a c) b}}', array('a' => 'A', 'b' => 'B', 'c' => 'C'))); $this->assertEquals('ACB', $engine->render('{{concat (concat a c) b}}', array('a' => 'A', 'b' => 'B', 'c' => 'C')));
$this->assertEquals('A-B', $engine->render('{{concat (concat a "-") b}}', array('a' => 'A', 'b' => 'B'))); $this->assertEquals('A-B', $engine->render('{{concat (concat a "-") b}}', array('a' => 'A', 'b' => 'B')));
$this->assertEquals('!B', $engine->render('{{concat (concat a "-") b}}', array('a' => 'A', 'b' => 'B', 'A-' => '!'))); $this->assertEquals('A-B', $engine->render('{{concat (concat a "-") b}}', array('a' => 'A', 'b' => 'B', 'A-' => '!')));
} }
/** /**