Prechádzať zdrojové kódy

Merge branch 'fix/nested-partial-whitespace' into dev

Justin Hileman 11 rokov pred
rodič
commit
c3e976a328

+ 9 - 2
src/Mustache/Compiler.php

@@ -401,9 +401,10 @@ class Mustache_Compiler
         return sprintf($this->prepare(self::INVERTED_SECTION, $level), $id, $method, $id, $filters, $this->walk($nodes, $level));
     }
 
+    const PARTIAL_INDENT = ', $indent . %s';
     const PARTIAL = '
         if ($partial = $this->mustache->loadPartial(%s)) {
-            $buffer .= $partial->renderInternal($context, $indent . %s);
+            $buffer .= $partial->renderInternal($context%s);
         }
     ';
 
@@ -418,10 +419,16 @@ class Mustache_Compiler
      */
     private function partial($id, $indent, $level)
     {
+        if ($indent !== '') {
+            $indentParam = sprintf(self::PARTIAL_INDENT, var_export($indent, true));
+        } else {
+            $indentParam = '';
+        }
+
         return sprintf(
             $this->prepare(self::PARTIAL, $level),
             var_export($id, true),
-            var_export($indent, true)
+            $indentParam
         );
     }
 

+ 45 - 0
test/Mustache/Test/Functional/NestedPartialIndentTest.php

@@ -0,0 +1,45 @@
+<?php
+
+/*
+ * This file is part of Mustache.php.
+ *
+ * (c) 2010-2014 Justin Hileman
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * @group functional
+ * @group partials
+ */
+class Mustache_Test_Functional_NestedPartialIndentTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * @dataProvider partialsAndStuff
+     */
+    public function testNestedPartialsAreIndentedProperly($src, array $partials, $expected)
+    {
+        $m = new Mustache_Engine(array(
+            'partials' => $partials
+        ));
+        $tpl = $m->loadTemplate($src);
+        $this->assertEquals($expected, $tpl->render());
+    }
+
+    public function partialsAndStuff()
+    {
+        $partials = array(
+            'a' => ' {{> b }}',
+            'b' => ' {{> d }}',
+            'c' => ' {{> d }}{{> d }}',
+            'd' => 'D!',
+        );
+
+        return array(
+            array(' {{> a }}', $partials, '   D!'),
+            array(' {{> b }}', $partials, '  D!'),
+            array(' {{> c }}', $partials, '  D!D!'),
+        );
+    }
+}