added an exception when a child template has a non-empty body (as it is always ignore...
authorFabien Potencier <fabien.potencier@gmail.com>
Thu, 20 May 2010 08:43:29 +0000 (10:43 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Thu, 20 May 2010 08:43:29 +0000 (10:43 +0200)
CHANGELOG
lib/Twig/Node/Module.php
lib/Twig/Parser.php

index c71ea40..e9af763 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,7 @@
 * 0.9.7-DEV
 
+ * added an exception when a child template has a non-empty body (as it is always ignored when rendering)
+
 * 0.9.6 (2010-05-12)
 
  * fixed variables defined outside a loop and for which the value changes in a for loop
index 3bbca1b..258f008 100644 (file)
@@ -42,7 +42,14 @@ class Twig_Node_Module extends Twig_Node implements Twig_NodeListInterface
 
     public function __toString()
     {
-        $repr = array(get_class($this).'(', '  body:');
+        $repr = array(get_class($this).'(');
+
+        if ($this->extends)
+        {
+            $repr[] = '  extends: '.$this->extends;
+        }
+
+        $repr[] = '  body:';
         foreach ($this->body->getNodes() as $node) {
             foreach (explode("\n", $node->__toString()) as $line) {
                 $repr[] = '    '.$line;
index a0dc62e..756b7af 100644 (file)
@@ -79,6 +79,21 @@ class Twig_Parser
             }
         }
 
+        if ($this->extends)
+        {
+            // check that the body only contains block references and empty text nodes
+            foreach ($body->getNodes() as $node)
+            {
+                if (
+                    ($node instanceof Twig_Node_Text && !preg_match('/^\s*$/s', $node->getData()))
+                    ||
+                    (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference)
+                ) {
+                    throw new Twig_SyntaxError('A template that extends another one cannot have a body', 0);
+                }
+            }
+        }
+
         $node = new Twig_Node_Module($body, $this->extends, $this->blocks, $this->macros, $this->stream->getFilename());
 
         $t = new Twig_NodeTraverser($this->env);