diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index c45ef87..79a7f70 100644 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -203,6 +203,10 @@ class Context return ''; } + if (substr($variableName, 0, 6) == '@root.') { + $variableName = trim(substr($variableName, 6)); + $level = count($this->stack)-1; + } end($this->stack); while ($level) { prev($this->stack); diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index a60ae3d..4b87caa 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -1226,6 +1226,33 @@ EOM; $this->assertEquals($res, $results); } + 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"), + ); + } + + /** + * Test 'root' special variable + * + * @param string $template template text + * @param array $data context data + * @param string $results The Expected Results + * + * @dataProvider rootSpecialVariableProvider + * + * @return void + */ + public function testRootSpecialVariableHelpers($template, $data, $results) + { + $engine = new \Handlebars\Handlebars(); + + $res = $engine->render($template, $data); + $this->assertEquals($res, $results); + } + } /**