Skip to content

Commit

Permalink
handle true and false for custom helper named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordius Chen committed Jun 30, 2014
1 parent 83ac359 commit a1e8067
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/lightncandy.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,8 @@ protected static function getVariableNames($vn, $context) {
* @return array variable names
*
* @expect Array('$in', 'this') when input Array(null), Array('flags'=>Array('spvar'=>true,'debug'=>0))
* @expect Array('true', 'true') when input Array('true'), Array('flags'=>Array('spvar'=>true,'debug'=>0))
* @expect Array('false', 'false') when input Array('false'), Array('flags'=>Array('spvar'=>true,'debug'=>0))
* @expect Array('((is_array($in) && isset($in[\'@index\'])) ? $in[\'@index\'] : null)', '[@index]') when input Array('@index'), Array('flags'=>Array('spvar'=>false,'debug'=>0))
* @expect Array('$cx[\'sp_vars\'][\'index\']', '@index') when input Array('@index'), Array('flags'=>Array('spvar'=>true,'debug'=>0))
* @expect Array('$cx[\'sp_vars\'][\'key\']', '@key') when input Array('@key'), Array('flags'=>Array('spvar'=>true,'debug'=>0))
Expand All @@ -774,6 +776,14 @@ protected static function getVariableName($var, &$context) {
}
}

// Handle language constants
switch ($var[0]) {
case 'true':
return Array('true', 'true');
case 'false':
return Array('false', 'false');
}

// Handle double quoted string
if (preg_match('/^"(.*)"$/', $var[0], $matched)) {
return Array("'{$matched[1]}'", $var[0]);
Expand Down
6 changes: 6 additions & 0 deletions tests/LightnCandyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ public function testOn_getVariableName() {
$this->assertEquals(Array('$in', 'this'), $method->invoke(null,
Array(null), Array('flags'=>Array('spvar'=>true,'debug'=>0))
));
$this->assertEquals(Array('true', 'true'), $method->invoke(null,
Array('true'), Array('flags'=>Array('spvar'=>true,'debug'=>0))
));
$this->assertEquals(Array('false', 'false'), $method->invoke(null,
Array('false'), Array('flags'=>Array('spvar'=>true,'debug'=>0))
));
$this->assertEquals(Array('((is_array($in) && isset($in[\'@index\'])) ? $in[\'@index\'] : null)', '[@index]'), $method->invoke(null,
Array('@index'), Array('flags'=>Array('spvar'=>false,'debug'=>0))
));
Expand Down
15 changes: 13 additions & 2 deletions tests/example_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function helper1 ($args, $named) {
// Custom Helper Interface ... named arguments
// Template: {{helper1 url=article.url text=article.text [ur"l]=article.extra}}
function helper2 ($args, $named) {
$u = isset($named['url']) ? $named['url'] : 'undefined';
$t = isset($named['text']) ? $named['text'] : 'undefined';
$u = isset($named['url']) ? jsraw($named['url']) : 'undefined';
$t = isset($named['text']) ? jsraw($named['text']) : 'undefined';
$x = isset($named['ur"l']) ? $named['ur"l'] : 'undefined';
return "<a href=\"{$u}\">{$t}</a>({$x})";
}
Expand Down Expand Up @@ -44,4 +44,15 @@ function myeach ($list, $options) {
return $ret;
}

// Simulate Javascript toString() behaviors
function jsraw ($i) {
if ($i === true) {
return 'true';
}
if ($i === false) {
return 'false';
}
return $i;
}

?>

0 comments on commit a1e8067

Please sign in to comment.