From 635bafd46e4f437ae7a215991f5f09839917f9e5 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 27 Apr 2012 09:16:21 +0200 Subject: [PATCH] fixed embed compilation when used more than once in a template (closes #714) --- lib/Twig/Node/Module.php | 2 +- lib/Twig/Parser.php | 3 +- test/Twig/Tests/Fixtures/tags/embed/multiple.test | 50 +++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tags/embed/multiple.test diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 2b8ae2f..4c7a107 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -18,7 +18,7 @@ */ class Twig_Node_Module extends Twig_Node { - public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, Twig_NodeInterface $embeddedTemplates, $filename) + public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $filename) { // embedded templates are set as attributes so that they are only visited once by the visitors parent::__construct(array('parent' => $parent, 'body' => $body, 'blocks' => $blocks, 'macros' => $macros, 'traits' => $traits), array('filename' => $filename, 'index' => null, 'embedded_templates' => $embeddedTemplates), 1); diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 6e5648c..0bfcec7 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -92,6 +92,7 @@ class Twig_Parser implements Twig_ParserInterface $this->traits = array(); $this->blockStack = array(); $this->importedFunctions = array(array()); + $this->embeddedTemplates = array(); try { $body = $this->subparse($test, $dropNeedle); @@ -109,7 +110,7 @@ class Twig_Parser implements Twig_ParserInterface throw $e; } - $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), new Twig_Node($this->embeddedTemplates), $this->stream->getFilename()); + $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $this->stream->getFilename()); $traverser = new Twig_NodeTraverser($this->env, $this->visitors); diff --git a/test/Twig/Tests/Fixtures/tags/embed/multiple.test b/test/Twig/Tests/Fixtures/tags/embed/multiple.test new file mode 100644 index 0000000..da161e6 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/embed/multiple.test @@ -0,0 +1,50 @@ +--TEST-- +"embed" tag +--TEMPLATE-- +FOO +{% embed "foo.twig" %} + {% block c1 %} + {{ parent() }} + block1extended + {% endblock %} +{% endembed %} + +{% embed "foo.twig" %} + {% block c1 %} + {{ parent() }} + block1extended + {% endblock %} +{% endembed %} + +BAR +--TEMPLATE(foo.twig)-- +A +{% block c1 %} + block1 +{% endblock %} +B +{% block c2 %} + block2 +{% endblock %} +C +--DATA-- +return array() +--EXPECT-- +FOO + +A + block1 + + block1extended + B + block2 +C + +A + block1 + + block1extended + B + block2 +C +BAR -- 1.7.2.5