From a9082dd29f436d5d26ea9a6e769e2a962fa51227 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 19 Sep 2016 16:24:27 -0700 Subject: [PATCH 1/2] fixed typo --- CHANGELOG | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 61d8ed195a..32a73f9e67 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,7 +7,7 @@ * 1.24.2 (2016-09-01) * fixed static callables - * fixed a potential PHP warning when load the cache + * fixed a potential PHP warning when loading the cache * fixed a case where the autoescaping does not work as expected * 1.24.1 (2016-05-30) From a0489854ba288585de7085014212b1ccb13d27f1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 19 Sep 2016 16:20:41 -0700 Subject: [PATCH 2/2] fixed embed parent token for simple use cases --- CHANGELOG | 1 + lib/Twig/TokenParser/Embed.php | 13 +++++-- .../tags/embed/complex_dynamic_parent.test | 35 +++++++++++++++++++ .../Fixtures/tags/embed/dynamic_parent.test | 35 +++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tags/embed/complex_dynamic_parent.test create mode 100644 test/Twig/Tests/Fixtures/tags/embed/dynamic_parent.test diff --git a/CHANGELOG b/CHANGELOG index 32a73f9e67..dd27c84c34 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.25.0 (2016-XX-XX) + * removed embed parent workaround for simple use cases * deprecated the ability to store non Node instances in Node::$nodes * deprecated Twig_Environment::getLexer(), Twig_Environment::getParser(), Twig_Environment::getCompiler() * deprecated Twig_Compiler::getFilename() diff --git a/lib/Twig/TokenParser/Embed.php b/lib/Twig/TokenParser/Embed.php index e685b955de..150f433bef 100644 --- a/lib/Twig/TokenParser/Embed.php +++ b/lib/Twig/TokenParser/Embed.php @@ -22,18 +22,27 @@ public function parse(Twig_Token $token) list($variables, $only, $ignoreMissing) = $this->parseArguments(); + $parentToken = $fakeParentToken = new Twig_Token(Twig_Token::STRING_TYPE, '__parent__', $token->getLine()); + if ($parent instanceof Twig_Node_Expression_Constant) { + $parentToken = new Twig_Token(Twig_Token::STRING_TYPE, $parent->getAttribute('value'), $token->getLine()); + } elseif ($parent instanceof Twig_Node_Expression_Name) { + $parentToken = new Twig_Token(Twig_Token::NAME_TYPE, $parent->getAttribute('name'), $token->getLine()); + } + // inject a fake parent to make the parent() function work $stream->injectTokens(array( new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', $token->getLine()), new Twig_Token(Twig_Token::NAME_TYPE, 'extends', $token->getLine()), - new Twig_Token(Twig_Token::STRING_TYPE, '__parent__', $token->getLine()), + $parentToken, new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', $token->getLine()), )); $module = $this->parser->parse($stream, array($this, 'decideBlockEnd'), true); // override the parent with the correct one - $module->setNode('parent', $parent); + if ($fakeParentToken === $parentToken) { + $module->setNode('parent', $parent); + } $this->parser->embedTemplate($module); diff --git a/test/Twig/Tests/Fixtures/tags/embed/complex_dynamic_parent.test b/test/Twig/Tests/Fixtures/tags/embed/complex_dynamic_parent.test new file mode 100644 index 0000000000..de5ea7eea3 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/embed/complex_dynamic_parent.test @@ -0,0 +1,35 @@ +--TEST-- +"embed" tag +--TEMPLATE-- +FOO +{% 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('foo' => 'foo') +--EXPECT-- +FOO + +A + block1 + + block1extended + B + block2 +C +BAR diff --git a/test/Twig/Tests/Fixtures/tags/embed/dynamic_parent.test b/test/Twig/Tests/Fixtures/tags/embed/dynamic_parent.test new file mode 100644 index 0000000000..2a125e6b58 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/embed/dynamic_parent.test @@ -0,0 +1,35 @@ +--TEST-- +"embed" tag +--TEMPLATE-- +FOO +{% embed foo %} + {% block c1 %} + {{ parent() }} + block1extended + {% endblock %} +{% endembed %} + +BAR +--TEMPLATE(foo.twig)-- +A +{% block c1 %} + block1 +{% endblock %} +B +{% block c2 %} + block2 +{% endblock %} +C +--DATA-- +return array('foo' => 'foo.twig') +--EXPECT-- +FOO + +A + block1 + + block1extended + B + block2 +C +BAR