From e5c03d3b01da56a54bbea5622ff64cede4895ca5 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Wed, 16 Nov 2016 21:52:19 +0100 Subject: [PATCH] properly pop the context for fn and inverse callbacks fixes #160. --- src/Handlebars/Handlebars.php | 4 ++-- tests/Xamin/HandlebarsTest.php | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Handlebars/Handlebars.php b/src/Handlebars/Handlebars.php index acf8193..09a2e1b 100755 --- a/src/Handlebars/Handlebars.php +++ b/src/Handlebars/Handlebars.php @@ -335,7 +335,7 @@ class Handlebars //I mean in this context (literally) //$template->discard($inContext); - if ($defined) { + if (!$defined) { $inContext->pop(); } @@ -359,7 +359,7 @@ class Handlebars $template->setStopToken(false); $buffer = $template->render($inContext); - if ($defined) { + if (!$defined) { $inContext->pop(); } diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index fa6521a..8defd75 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -592,9 +592,9 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $expected1 = 'bonjour, mon nom est Foo! comment sont les enfants de votre 6 et dog?'; //case 2 - when - $variable2 = array('gender' => 'female'); - $template2 = "Hello {{#when gender '===' 'male'}}sir{{else}}maam{{/when}}"; - $expected2 = 'Hello maam'; + $variable2 = array('gender' => 'female', 'foo' => 'bar'); + $template2 = "Hello {{#when gender '===' 'male'}}sir{{else}}maam{{/when}} {{foo}}"; + $expected2 = 'Hello maam bar'; //case 3 - when else $variable3 = array('gender' => 'male'); @@ -621,14 +621,15 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $variable5 = $variable4; $variable5['me'] = 'Jack Doe'; $variable5['admins'] = array('Jane Doe', 'John Doe'); - $template5 = "{{#in admins me}}{{else}}No Access{{/in}}"; + $template5 = "{{#in admins me}}{{else}}No Access{{/in}}{{suffix}}"; $expected5 = 'No Access'; //case 6 - array in else $variable6 = $variable5; $variable6['me'] = 'Jane Doe'; + $variable6['suffix'] = 'qux'; $template6 = $template5; - $expected6 = ''; + $expected6 = 'qux'; //case 7 - nested templates and parent-grand variables $variable7 = array('test' => 'Hello World'); @@ -638,6 +639,16 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase .'In 3: {{test6}} {{../../../test}}{{/nested2}}{{/nested1}}{{/nested1}}'; $expected7 = ' In 1: Hello World In 2: This is Test 5 In 3: This is Test 6 Hello World'; + //case 8 - when inside an each + $variable8 = array('data' => array(0, 1, 2, 3),'finish' => 'ok'); + $template8 = '{{#each data}}{{#when this ">" "0"}}{{this}}{{/when}}{{/each}} {{finish}}'; + $expected8 = '123 ok'; + + //case 9 - when inside an each + $variable9 = array('data' => array(),'finish' => 'ok'); + $template9 = '{{#each data}}{{#when this ">" "0"}}{{this}}{{/when}}{{else}}foo{{/each}} {{finish}}'; + $expected9 = 'foo ok'; + //LAST UP: the actual testing $this->assertEquals($expected1, $engine->render($template1, $variable1)); @@ -647,6 +658,8 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected5, $engine->render($template5, $variable5)); $this->assertEquals($expected6, $engine->render($template6, $variable6)); $this->assertEquals($expected7, $engine->render($template7, $variable7)); + $this->assertEquals($expected8, $engine->render($template8, $variable8)); + $this->assertEquals($expected9, $engine->render($template9, $variable9)); } public function testInvalidHelper()