diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 5dbd515..f7ab018 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -354,9 +354,9 @@ class Template Tokenizer::ARGS => implode(" ", array_slice($cmd, 1)) ); // resolve the node recursively - $resolved = $this->_handlebarsStyleSection($context, $section_node); + $resolved = addcslashes($this->_handlebarsStyleSection($context, $section_node), '"'); // 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]); } } diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 3207112..2e56211 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -1049,35 +1049,37 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase { $loader = new \Handlebars\Loader\StringLoader(); $engine = new \Handlebars\Handlebars(array('loader' => $loader)); - $engine->addHelper('test', function ($template, $context, $arg) { - return $arg . 'Test.'; + $engine->addHelper('test', function ($template, $context, $args) { + $parsedArgs = $template->parseArguments($args); + + return (count($parsedArgs) ? $context->get($parsedArgs[0]) : '') . 'Test.'; }); // assert that nested syntax is accepted and sub-helper is run $this->assertEquals('Test.Test.', $engine->render('{{test (test)}}', array())); - $engine->addHelper('add', function ($template, $context, $arg) { - $values = explode(" ", $arg); + $engine->addHelper('add', function ($template, $context, $args) { + $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 $this->assertEquals('42', $engine->render('{{add 21 (add 10 (add 5 6))}}', array())); // 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) { $result = ''; foreach($template->parseArguments($args) as $arg) { - if (($trnaslated = $context->get($arg)) !== null ) { - $result .= $trnaslated; - } else { - $result .= $arg; - } + $result .= $context->get($arg); } 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 (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('!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-' => '!'))); } /**