From: Fabien Potencier Date: Fri, 20 Apr 2012 09:17:22 +0000 (+0200) Subject: added an error when defining two blocks with the same name in a template (closes... X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=0add6e23771bf44d8ee643c555597b87ae4a598b;p=web%2Fkonrad%2Ftwig.git added an error when defining two blocks with the same name in a template (closes #701) --- diff --git a/CHANGELOG b/CHANGELOG index 1613419..0badb9e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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() diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 72d3637..e8db493 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -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) diff --git a/lib/Twig/TokenParser/Block.php b/lib/Twig/TokenParser/Block.php index 36643f7..994078e 100644 --- a/lib/Twig/TokenParser/Block.php +++ b/lib/Twig/TokenParser/Block.php @@ -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(); diff --git a/test/Twig/Tests/Fixtures/tags/block/block_unique_name.test b/test/Twig/Tests/Fixtures/tags/block/block_unique_name.test new file mode 100644 index 0000000..5c205c0 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/block/block_unique_name.test @@ -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