Merge pull request #137 from stevejordan/if_unless_depth_correction

Updates if and unless helper behavior for handlebars 4
This commit is contained in:
Behrooz Shabani 2016-07-31 12:16:29 +02:00 committed by GitHub
commit fe4e79ed83
3 changed files with 22 additions and 10 deletions

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -1383,16 +1383,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));
}
/**