From a44777f52f426e3a384c6b8b4df1d49a87587715 Mon Sep 17 00:00:00 2001 From: Chris Gray Date: Mon, 20 Jan 2014 09:57:16 -0600 Subject: [PATCH] Added Tests and changed internal representation of quoted string literals from strangely quoted string to an instance of \Handlebars\String --- src/Handlebars/Context.php | 7 +++--- src/Handlebars/Template.php | 1 - tests/Xamin/HandlebarsTest.php | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index 66d8742..85312c1 100755 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -189,7 +189,9 @@ class Context */ public function get($variableName, $strict = false) { - //Need to clean up + if ($variableName instanceof \Handlebars\String){ + return (string)$variableName; + } $variableName = trim($variableName); $level = 0; while (substr($variableName, 0, 3) == '../') { @@ -217,7 +219,6 @@ class Context 'can not find variable in context' ); } - return ''; } elseif ($variableName == '.' || $variableName == 'this') { return $current; @@ -225,8 +226,6 @@ class Context $current = $this->lastIndex(); } elseif ($variableName == '@key') { $current = $this->lastKey(); - } elseif ($variableName instanceof \Handlebars\String){ - $current = (string)$variableName; } else { $chunks = explode('.', $variableName); foreach ($chunks as $chunk) { diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index eb791b3..54e8656 100755 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -446,7 +446,6 @@ class Template } } - return $args; } } diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 7680250..eb96579 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -94,6 +94,16 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase '{{data.length}}', array("data"=>array("length"=>"15 inches", "test","test","test")), "15 inches" + ), + array( + '{{data.0}}', + array("data" => array(1,2,3,4)), + '1' + ), + array( + '{{data.property.3}}', + array("data"=>array("property"=>array(1,2,3,4))), + '4' ) ); } @@ -527,6 +537,41 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase } + public function stringLiteralInCustomHelperProvider(){ + return array( + array('{{#test2 arg1 "Argument 2"}}', array("arg1"=>"Argument 1"), "Argument 1:Argument 2"), + array('{{#test2 "Argument 1" "Argument 2"}}', array("arg1"=>"Argument 1"), "Argument 1:Argument 2"), + array('{{#test2 "Argument 1" arg2}}', array("arg2"=>"Argument 2"), "Argument 1:Argument 2") + ); + } + /** + * Test String literals in the context of a helper + * + * @param string $template template text + * @param array $data context data + * @param string $results The Expected Results + * + * @dataProvider stringLiteralInCustomHelperProvider + * + * @return void + */ + public function testStringLiteralInCustomHelper($template, $data, $results){ + $engine = new \Handlebars\Handlebars(); + $engine->addHelper('test2', function ($template, $context, $args) { + $args = $template->parseArguments($args); + + $args = array_map(function ($a) use ($context) {return $context->get($a);}, $args); + return implode(':', $args); + }); + $res = $engine->render($template, $data); + $this->assertEquals($res, $results); + } + + public function testString(){ + $string = new \Handlebars\String("Hello World"); + $this->assertEquals((string)$string, "Hello World"); + } + } \ No newline at end of file