From a1e80675292e76596f20ba4abda4e8b118ceef38 Mon Sep 17 00:00:00 2001 From: Zordius Chen Date: Mon, 30 Jun 2014 17:01:38 +0800 Subject: [PATCH] handle true and false for custom helper named arguments --- src/lightncandy.php | 10 ++++++++++ tests/LightnCandyTest.php | 6 ++++++ tests/example_helpers.php | 15 +++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/lightncandy.php b/src/lightncandy.php index c31d1ca3..84e6b6a0 100644 --- a/src/lightncandy.php +++ b/src/lightncandy.php @@ -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)) @@ -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]); diff --git a/tests/LightnCandyTest.php b/tests/LightnCandyTest.php index f255a45f..27aabb43 100644 --- a/tests/LightnCandyTest.php +++ b/tests/LightnCandyTest.php @@ -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)) )); diff --git a/tests/example_helpers.php b/tests/example_helpers.php index 77b211bc..f5e67d6f 100755 --- a/tests/example_helpers.php +++ b/tests/example_helpers.php @@ -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 "{$t}({$x})"; } @@ -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; +} + ?>