added an error when defining two blocks with the same name in a template (closes #701)

This commit is contained in:
Fabien Potencier
2012-04-20 11:17:22 +02:00
parent 6cf147dc99
commit 0add6e2377
4 changed files with 21 additions and 4 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.7.0 (2012-XX-XX)
* added an error when defining two blocks with the same name in a template
* fixed a PHP notice when trying to access a key on a non-object/array variable
* enhanced error reporting when the template file is an instance of SplFileInfo
* added Twig_Environment::mergeGlobals()
+6 -1
View File
@@ -229,9 +229,14 @@ class Twig_Parser implements Twig_ParserInterface
return isset($this->blocks[$name]);
}
public function getBlock($name)
{
return $this->blocks[$name];
}
public function setBlock($name, $value)
{
$this->blocks[$name] = new Twig_Node_Body(array($value));
$this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getLine());
}
public function hasMacro($name)
+3 -3
View File
@@ -35,8 +35,9 @@ class Twig_TokenParser_Block extends Twig_TokenParser
$stream = $this->parser->getStream();
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
if ($this->parser->hasBlock($name)) {
throw new Twig_Error_Syntax("The block '$name' has already been defined", $lineno);
throw new Twig_Error_Syntax(sprintf("The block '$name' has already been defined line %d", $this->parser->getBlock($name)->getLine()), $lineno);
}
$this->parser->setBlock($name, $block = new Twig_Node_Block($name, new Twig_Node(array()), $lineno));
$this->parser->pushLocalScope();
$this->parser->pushBlockStack($name);
@@ -58,8 +59,7 @@ class Twig_TokenParser_Block extends Twig_TokenParser
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$block = new Twig_Node_Block($name, $body, $lineno);
$this->parser->setBlock($name, $block);
$block->setNode('body', $body);
$this->parser->popBlockStack();
$this->parser->popLocalScope();
@@ -0,0 +1,11 @@
--TEST--
"block" tag
--TEMPLATE--
{% block content %}
{% block content %}
{% endblock %}
{% endblock %}
--DATA--
return array()
--EXCEPTION--
Twig_Error_Syntax: The block 'content' has already been defined line 2 in "index.twig" at line 3