diff --git a/src/Handlebars/Helper/IfHelper.php b/src/Handlebars/Helper/IfHelper.php index 9d742db..73e3a53 100644 --- a/src/Handlebars/Helper/IfHelper.php +++ b/src/Handlebars/Helper/IfHelper.php @@ -53,7 +53,6 @@ class IfHelper implements Helper $parsedArgs = $template->parseArguments($args); $tmp = $context->get($parsedArgs[0]); - $context->push($context->last()); if ($tmp) { $template->setStopToken('else'); $buffer = $template->render($context); @@ -65,7 +64,6 @@ class IfHelper implements Helper $template->setStopToken(false); $buffer = $template->render($context); } - $context->pop(); return $buffer; } diff --git a/src/Handlebars/Helper/UnlessHelper.php b/src/Handlebars/Helper/UnlessHelper.php index bab6ca4..2465131 100644 --- a/src/Handlebars/Helper/UnlessHelper.php +++ b/src/Handlebars/Helper/UnlessHelper.php @@ -53,8 +53,6 @@ class UnlessHelper implements Helper $parsedArgs = $template->parseArguments($args); $tmp = $context->get($parsedArgs[0]); - $context->push($context->last()); - if (!$tmp) { $template->setStopToken('else'); $buffer = $template->render($context); @@ -66,8 +64,6 @@ class UnlessHelper implements Helper $buffer = $template->render($context); } - $context->pop(); - return $buffer; } } diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 1d91e1b..21c3919 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -1174,16 +1174,34 @@ EOM; $this->assertEquals('A-B', $engine->render('{{concat (concat a "-") b}}', array('a' => 'A', 'b' => 'B', 'A-' => '!'))); } + public function ifUnlessDepthDoesntChangeProvider() + { + return [[ + '{{#with b}}{{#if this}}{{../a}}{{/if}}{{/with}}', + ['a' => 'good', 'b' => 'stump'], + 'good', + ], [ + '{{#with b}}{{#unless false}}{{../a}}{{/unless}}{{/with}}', + ['a' => 'good', 'b' => 'stump'], + 'good', + ], [ + '{{#with foo}}{{#if goodbye}}GOODBYE cruel {{../world}}!{{/if}}{{/with}}', + ['foo' => ['goodbye' => true], 'world' => 'world'], + 'GOODBYE cruel world!', + ]]; + } + /** - * Test if and unless adding an extra layer when accessing parent + * Test if and unless do not add an extra layer when accessing parent + * + * @dataProvider ifUnlessDepthDoesntChangeProvider */ - public function testIfUnlessExtraLayer() + public function testIfUnlessDepthDoesntChange($template, $data, $expected) { $loader = new \Handlebars\Loader\StringLoader(); $engine = new \Handlebars\Handlebars(array('loader' => $loader)); - $this->assertEquals('good', $engine->render('{{#with b}}{{#if this}}{{../../a}}{{/if}}{{/with}}', array('a' => 'good', 'b' => 'stump'))); - $this->assertEquals('good', $engine->render('{{#with b}}{{#unless false}}{{../../a}}{{/unless}}{{/with}}', array('a' => 'good', 'b' => 'stump'))); + $this->assertEquals($expected, $engine->render($template, $data)); } /**