Skip to content

Commit

Permalink
bug #2124 fixed embed parent token for simple use cases (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

fixed embed parent token for simple use cases

Not the most elegant solution but solves the problem for when `embed` is used on a non-dynamic template, which should cover almost all use cases.

Commits
-------

a048985 fixed embed parent token for simple use cases
a9082dd fixed typo
  • Loading branch information
fabpot committed Sep 19, 2016
2 parents 75482cf + a048985 commit 0da2dd4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
@@ -1,13 +1,14 @@
* 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()

* 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)
Expand Down
13 changes: 11 additions & 2 deletions lib/Twig/TokenParser/Embed.php
Expand Up @@ -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);

Expand Down
35 changes: 35 additions & 0 deletions 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
35 changes: 35 additions & 0 deletions 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

0 comments on commit 0da2dd4

Please sign in to comment.