fixed a bug when a template with a layout is reused more than one time
authorFabien Potencier <fabien.potencier@gmail.com>
Fri, 11 Jun 2010 10:10:32 +0000 (12:10 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Fri, 11 Jun 2010 10:10:32 +0000 (12:10 +0200)
lib/Twig/Node/Module.php
test/Twig/Tests/Node/ModuleTest.php
test/Twig/Tests/Node/SandboxedModuleTest.php

index ed98ac6..eeaddc8 100644 (file)
@@ -61,6 +61,11 @@ class Twig_Node_Module extends Twig_Node
                 }
             }
 
+            $compiler
+                ->write("if (null === \$this->parent) {\n")
+                ->indent();
+            ;
+
             if ($this->parent instanceof Twig_Node_Expression_Constant) {
                 $compiler
                     ->write("\$this->parent = \$this->env->loadTemplate(")
@@ -83,6 +88,8 @@ class Twig_Node_Module extends Twig_Node
 
             $compiler
                 ->write("\$this->parent->pushBlocks(\$this->blocks);\n")
+                ->outdent()
+                ->write("}\n")
                 ->write("\$this->parent->display(\$context);\n")
             ;
         } else {
index 837a298..2e78f2e 100644 (file)
@@ -103,8 +103,10 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
     public function display(array \$context)
     {
         \$context['macro'] = \$this->env->loadTemplate("foo.twig", true);
-        \$this->parent = \$this->env->loadTemplate("layout.twig");
-        \$this->parent->pushBlocks(\$this->blocks);
+        if (null === \$this->parent) {
+            \$this->parent = \$this->env->loadTemplate("layout.twig");
+            \$this->parent->pushBlocks(\$this->blocks);
+        }
         \$this->parent->display(\$context);
     }
 
@@ -140,11 +142,13 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
 
     public function display(array \$context)
     {
-        \$this->parent = (true) ? ("foo") : ("foo");
-        if (!\$this->parent instanceof Twig_Template) {
-            \$this->parent = \$this->env->loadTemplate(\$this->parent);
+        if (null === \$this->parent) {
+            \$this->parent = (true) ? ("foo") : ("foo");
+            if (!\$this->parent instanceof Twig_Template) {
+                \$this->parent = \$this->env->loadTemplate(\$this->parent);
+            }
+            \$this->parent->pushBlocks(\$this->blocks);
         }
-        \$this->parent->pushBlocks(\$this->blocks);
         \$this->parent->display(\$context);
     }
 
index ac476db..4e2cd0b 100644 (file)
@@ -110,8 +110,10 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
 
     public function display(array \$context)
     {
-        \$this->parent = \$this->env->loadTemplate("layout.twig");
-        \$this->parent->pushBlocks(\$this->blocks);
+        if (null === \$this->parent) {
+            \$this->parent = \$this->env->loadTemplate("layout.twig");
+            \$this->parent->pushBlocks(\$this->blocks);
+        }
         \$this->parent->display(\$context);
     }