From 44baf86426397429f293b63cefb8dd298a986649 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Wed, 28 Jun 2017 23:20:21 -0700 Subject: [PATCH] Suppress `else` when rendering empty block defaults. --- src/Mustache/Compiler.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index 2a831d6..dd40d8a 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -240,10 +240,11 @@ class Mustache_Compiler $blockFunction = $context->findInBlock(%s); if (is_callable($blockFunction)) { $buffer .= call_user_func($blockFunction, $context); - } else {%s - } + %s} '; + const BLOCK_VAR_ELSE = '} else {%s'; + /** * Generate Mustache Template inheritance block variable PHP source. * @@ -261,7 +262,12 @@ class Mustache_Compiler { $id = var_export($id, true); - return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $this->walk($nodes, $level)); + $else = $this->walk($nodes, $level); + if ($else !== '') { + $else = sprintf($this->prepare(self::BLOCK_VAR_ELSE, $level + 1, false, true), $else); + } + + return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $else); } const BLOCK_ARG = '$blocksContext[%s] = array($this, \'block%s\');';