Skip to content

Commit

Permalink
handle standalone {{else}}
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordius Chen committed Aug 18, 2014
1 parent 9a23bb0 commit b6eecd3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/lightncandy.php
Original file line number Diff line number Diff line change
Expand Up @@ -1456,11 +1456,12 @@ public static function noNamedArguments($token, &$context, $named, $suggest = '!
* Internal method used by compileToken(). Modify $token when mustache rules matched.
*
* @param array<string> $token detected handlebars {{ }} token
* @param array<array|string|integer> $vars parsed arguments list
* @param array<string,array|string|integer> $context current compile context
*
* @return string|null Return compiled code segment for the token
*/
public static function handleMustacheSpacing(&$token, &$context) {
public static function handleMustacheSpacing(&$token, $vars, &$context) {
// Line change detection
$lsp = preg_match('/^(.*)(\\r?\\n)([ \\t]*?)$/s', $token[self::POS_LSPACE], $lmatch);
$rsp = preg_match('/^([ \\t]*?)(\\r?\\n)(.*)$/s', $token[self::POS_RSPACE], $rmatch);
Expand All @@ -1479,7 +1480,9 @@ public static function handleMustacheSpacing(&$token, &$context) {

// Do need standalone detection for these tags
if (!$token[self::POS_OP] || ($token[self::POS_OP] === '&')) {
return;
if (!$context['flags']['else'] || (isset($vars[0][0]) && ($vars[0][0] !== 'else'))) {
return;
}
}

// not standalone because other things in the same line ahead
Expand Down Expand Up @@ -1519,7 +1522,7 @@ public static function compileToken(&$token, &$context) {

// Handle Mustache spacing
if ($context['flags']['mustsp']) {
static::handleMustacheSpacing($token, $context);
static::handleMustacheSpacing($token, $vars, $context);
}

// Handle space control.
Expand Down
7 changes: 7 additions & 0 deletions tests/regressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,13 @@ public function issueProvider()
'expected' => "OK\n A\n d\n E\n ",
),

Array(
'template' => " {{#if foo}}\nYES\n{{else}}\nNO\n{{/if}}\n",
'data' => null,
'options' => Array('flags' => LightnCandy::FLAG_MUSTACHESP | LightnCandy::FLAG_ELSE),
'expected' => "NO\n",
),

);

return array_map(function($i) {
Expand Down

0 comments on commit b6eecd3

Please sign in to comment.