Przeglądaj źródła

Suppress `else` when rendering empty block defaults.

Justin Hileman 8 lat temu
rodzic
commit
44baf86426
1 zmienionych plików z 9 dodań i 3 usunięć
  1. 9 3
      src/Mustache/Compiler.php

+ 9 - 3
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\');';