From: Fabien Potencier Date: Thu, 20 May 2010 08:43:29 +0000 (+0200) Subject: added an exception when a child template has a non-empty body (as it is always ignore... X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=9c253d699bd22cce98cefc7e437d4f0aa6016e45;p=web%2Fkonrad%2Ftwig.git added an exception when a child template has a non-empty body (as it is always ignored when rendering) --- diff --git a/CHANGELOG b/CHANGELOG index c71ea40..e9af763 100644 --- 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 diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 3bbca1b..258f008 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -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; diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index a0dc62e..756b7af 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -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);