Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordius Chen committed Aug 26, 2014
1 parent 502c467 commit b211e17
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Default is to compile the template as PHP, which can be run as fast as possible
* `FLAG_ERROR_EXCEPTION` : throw exception when found any template error
* `FLAG_ERROR_SKIPPARTIAL` : skip 'partial not found' error/exception. Use this to align with mustache specification.
* `FLAG_STANDALONE` : generate stand-alone PHP codes, which can be execute without including LightnCandy.php. The compiled PHP code will contain scoped user function, somehow larger. And, the performance of the template will slow 1 ~ 10%.
* `FLAG_JSTRUE` : generate 'true' when value is true (JavaScript behavior). Otherwise, true will generate ''.
* `FLAG_JSTRUE` : generate 'true' or 'false' when value is true or false (JavaScript behavior). Otherwise, true/false will generate ''.
* `FLAG_JSOBJECT` : generate '[object Object]' for associated array, generate ',' separated values for array (JavaScript behavior). Otherwise, all PHP array will generate '' or 'Array'.
* `FLAG_THIS` : resolve `{{this}}` as `{{.}}` in template. Otherwise, `{{this}}` will be resolved as normal variable.
* `FLAG_WITH` : support `{{#with var}}` in template. Otherwise, `{{#with var}}` will cause template error.
Expand Down Expand Up @@ -589,17 +589,17 @@ Go http://handlebarsjs.com/ to see more feature description about handlebars.js.

* Exact same CR/LF behavior with handlebars.js
* Exact same CR/LF bahavior with mustache spec (require `FLAG_MUSTACHESP`)
* Exact same 'true' output with handlebars.js (require `FLAG_JSTRUE`)
* Exact same 'true' or 'false' output with handlebars.js (require `FLAG_JSTRUE`)
* Exact same '[object Object]' output or join(',' array) output with handlebars.js (require `FLAG_JSOBJECT`)
* Can place heading/tailing space, tab, CR/LF inside `{{ var }}` or `{{{ var }}}`
* Indent behavior of the partial same with mustache spec (require `FLAG_MUSTACHEPAIN`)
* Recursive variable lookup to parent context behavior same with mustache spec (require `FLAG_MUSTACHELOOKUP`)
* `{{{value}}}` or `{{&value}}` : raw variable
* true as 'true' (require `FLAG_JSTRUE`)
* false as ''
* false as 'false' (require `FLAG_TRUE`)
* `{{value}}` : HTML escaped variable
* true as 'true' (require `FLAG_JSTRUE`)
* false as ''
* false as 'false' (require `FLAG_JSTRUE`)
* `{{{path.to.value}}}` : dot notation, raw
* `{{path.to.value}}` : dot notation, HTML escaped
* `{{.}}` : current context, HTML escaped
Expand Down
25 changes: 4 additions & 21 deletions src/lightncandy.php
Original file line number Diff line number Diff line change
Expand Up @@ -1516,22 +1516,6 @@ public static function handleMustacheSpacing(&$token, $vars, &$context) {
}
if ($context['flags']['mustsp']) {
$token[self::POS_LSPACE] = (isset($lmatch[2]) ? ($lmatch[1] . $lmatch[2]) : '');
if ($token[self::POS_OP] !== ' ') {
if ($context['tokens']['standalone'] && !$token[self::POS_LOTHER]) {
$token[self::POS_LSPACE] = preg_replace('/^\n(\n?)$/s', '$1', $token[self::POS_LSPACE]);
} else {
$token[self::POS_LSPACE] = preg_replace('/^\n\n$/s', "\n", $token[self::POS_LSPACE]);
}
}
$token[self::POS_RSPACE] = isset($rmatch[3]) ? $rmatch[3] : '';
$context['tokens']['standalone'] = ($token[self::POS_OP] !== ' ');
}
} else {
$context['tokens']['standalone'] = false;
// Align with handlebars.js current behavior, not sure this is correct or not
// /~https://github.com/wycats/handlebars.js/issues/852
if ($token[self::POS_RSPACECTL]) {
$token[self::POS_RSPACECTL] = 0;
$token[self::POS_RSPACE] = isset($rmatch[3]) ? $rmatch[3] : '';
}
}
Expand Down Expand Up @@ -2068,14 +2052,13 @@ public static function isec($cx, $v) {
*
* @param array<string,array|string|integer> $cx render time context
* @param array<array|string|integer>|string|integer|null $v value to be output
* @param boolean $loop true when in loop
*
* @return string The raw value of the specified variable
*
* @expect true when input array('flags' => array('jstrue' => 0)), true
* @expect 'true' when input array('flags' => array('jstrue' => 1)), true
* @expect '' when input array('flags' => array('jstrue' => 0)), false
* @expect '' when input array('flags' => array('jstrue' => 1)), false
* @expect 'false' when input array('flags' => array('jstrue' => 1)), false
* @expect 'false' when input array('flags' => array('jstrue' => 1)), false, true
* @expect array('a', 'b') when input array('flags' => array('jstrue' => 1, 'jsobj' => 0)), array('a', 'b')
* @expect 'a,b' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1)), array('a', 'b')
Expand All @@ -2086,14 +2069,14 @@ public static function isec($cx, $v) {
* @expect 'a,' when input array('flags' => array('jstrue' => 0, 'jsobj' => 1)), array('a',false)
* @expect 'a,false' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1)), array('a',false)
*/
public static function raw($cx, $v, $loop = false) {
public static function raw($cx, $v) {
if ($v === true) {
if ($cx['flags']['jstrue']) {
return 'true';
}
}

if ($loop && ($v === false)) {
if (($v === false)) {
if ($cx['flags']['jstrue']) {
return 'false';
}
Expand All @@ -2106,7 +2089,7 @@ public static function raw($cx, $v, $loop = false) {
} else {
$ret = array();
foreach ($v as $k => $vv) {
$ret[] = self::raw($cx, $vv, true);
$ret[] = self::raw($cx, $vv);
}
return join(',', $ret);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/LCRun3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function testOn_raw() {
$this->assertEquals('', $method->invoke(null,
array('flags' => array('jstrue' => 0)), false
));
$this->assertEquals('', $method->invoke(null,
$this->assertEquals('false', $method->invoke(null,
array('flags' => array('jstrue' => 1)), false
));
$this->assertEquals('false', $method->invoke(null,
Expand Down
4 changes: 2 additions & 2 deletions tests/regressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,14 @@ public function issueProvider()
'template' => "A\n {{#if 1}} \n\na\n{{#with 2}}\n123\n\n{{/with}}\n{{/if}} \n \n\n456",
'data' => null,
'options' => Array('flags' => LightnCandy::FLAG_WITH | LightnCandy::FLAG_MUSTACHESP),
'expected' => "A\n\na\n123\n \n\n456",
'expected' => "A\n\na\n123\n\n \n\n456",
),

Array(
'template' => "\n{{#with 1}}\n\n{{#with 1}}\nb\n\n{{/with}}\n{{/with}}\nC",
'data' => null,
'options' => Array('flags' => LightnCandy::FLAG_WITH | LightnCandy::FLAG_MUSTACHESP),
'expected' => "b\nC",
'expected' => "\n\nb\n\nC",
),

);
Expand Down

0 comments on commit b211e17

Please sign in to comment.