added an exception when a child template has a non-empty body (as it is always ignored when rendering)

This commit is contained in:
Fabien Potencier
2010-05-20 10:43:29 +02:00
parent ac49dcb455
commit 9c253d699b
3 changed files with 25 additions and 1 deletions
+2
View File
@@ -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
+8 -1
View 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;
+15
View 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);