Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fzerorubigd committed Jan 29, 2015
2 parents 42a7ae7 + 8eb24bd commit 704da1e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/Handlebars/Helper/IfHelper.php
Expand Up @@ -50,11 +50,8 @@ class IfHelper implements Helper
*/
public function execute(Template $template, Context $context, $args, $source)
{
if (is_numeric($args)) {
$tmp = $args;
} else {
$tmp = $context->get($args);
}
$parsedArgs = $template->parseArguments($args);
$tmp = $context->get($parsedArgs[0]);

$context->push($context->last());
if ($tmp) {
Expand Down
3 changes: 2 additions & 1 deletion src/Handlebars/Helper/UnlessHelper.php
Expand Up @@ -50,7 +50,8 @@ class UnlessHelper implements Helper
*/
public function execute(Template $template, Context $context, $args, $source)
{
$tmp = $context->get($args);
$parsedArgs = $template->parseArguments($args);
$tmp = $context->get($parsedArgs[0]);

$context->push($context->last());

Expand Down
50 changes: 50 additions & 0 deletions tests/Xamin/HandlebarsTest.php
Expand Up @@ -1155,4 +1155,54 @@ public function testArgumentsString()
$args = new \Handlebars\Arguments($argsString);
$this->assertEquals($argsString, (string)$args);
}


public function stringLiteralsInIfAndUnlessHelpersProvider() {
return array(
// IfHelper
array('{{#if "truthyString"}}true{{else}}false{{/if}}', array(), "true"),
array("{{#if 'truthyStringSingleQuotes'}}true{{else}}false{{/if}}", array(), "true"),
array("{{#if ''}}true{{else}}false{{/if}}", array(), "false"),
array("{{#if '0'}}true{{else}}false{{/if}}", array(), "false"),
array("{{#if (add 0 1)}}true{{else}}false{{/if}}", array(), "true"),
array("{{#if (add 1 -1)}}true{{else}}false{{/if}}", array(), "false"),
// UnlessHelper
array('{{#unless "truthyString"}}true{{else}}false{{/unless}}', array(), "false"),
array("{{#unless 'truthyStringSingleQuotes'}}true{{else}}false{{/unless}}", array(), "false"),
array("{{#unless ''}}true{{else}}false{{/unless}}", array(), "true"),
array("{{#unless '0'}}true{{else}}false{{/unless}}", array(), "true"),
array("{{#unless (add 0 1)}}true{{else}}false{{/unless}}", array(), "false"),
array("{{#unless (add 1 -1)}}true{{else}}false{{/unless}}", array(), "true"),
);
}

/**
* Test string literals in the context of if and unless helpers
*
* @param string $template template text
* @param array $data context data
* @param string $results The Expected Results
*
* @dataProvider stringLiteralsInIfAndUnlessHelpersProvider
*
* @return void
*/
public function testStringLiteralsInIfAndUnlessHelpers($template, $data, $results)
{
$engine = new \Handlebars\Handlebars();

$engine->addHelper('add', function ($template, $context, $args) {
$sum = 0;

foreach ($template->parseArguments($args) as $value) {
$sum += intval($context->get($value));
}

return $sum;
});

$res = $engine->render($template, $data);
$this->assertEquals($res, $results);
}

}

0 comments on commit 704da1e

Please sign in to comment.