Updates if and unless helper behavior for handlebars 4

handlebars.js 4.0.0 changed the depth behaviour when using the if and
unless conditionals - https://github.com/wycats/handlebars.js/issues/1028

This commit changes the handlebars.php helpers to match.
This commit is contained in:
Steve Jordan 2015-10-26 11:48:28 +00:00
parent 5e1db1d1c7
commit 909df9ab58
3 changed files with 22 additions and 10 deletions

View File

@ -53,7 +53,6 @@ class IfHelper implements Helper
$parsedArgs = $template->parseArguments($args); $parsedArgs = $template->parseArguments($args);
$tmp = $context->get($parsedArgs[0]); $tmp = $context->get($parsedArgs[0]);
$context->push($context->last());
if ($tmp) { if ($tmp) {
$template->setStopToken('else'); $template->setStopToken('else');
$buffer = $template->render($context); $buffer = $template->render($context);
@ -65,7 +64,6 @@ class IfHelper implements Helper
$template->setStopToken(false); $template->setStopToken(false);
$buffer = $template->render($context); $buffer = $template->render($context);
} }
$context->pop();
return $buffer; return $buffer;
} }

View File

@ -53,8 +53,6 @@ class UnlessHelper implements Helper
$parsedArgs = $template->parseArguments($args); $parsedArgs = $template->parseArguments($args);
$tmp = $context->get($parsedArgs[0]); $tmp = $context->get($parsedArgs[0]);
$context->push($context->last());
if (!$tmp) { if (!$tmp) {
$template->setStopToken('else'); $template->setStopToken('else');
$buffer = $template->render($context); $buffer = $template->render($context);
@ -66,8 +64,6 @@ class UnlessHelper implements Helper
$buffer = $template->render($context); $buffer = $template->render($context);
} }
$context->pop();
return $buffer; return $buffer;
} }
} }

View File

@ -1174,16 +1174,34 @@ EOM;
$this->assertEquals('A-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-' => '!')));
} }
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(); $loader = new \Handlebars\Loader\StringLoader();
$engine = new \Handlebars\Handlebars(array('loader' => $loader)); $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($expected, $engine->render($template, $data));
$this->assertEquals('good', $engine->render('{{#with b}}{{#unless false}}{{../../a}}{{/unless}}{{/with}}', array('a' => 'good', 'b' => 'stump')));
} }
/** /**