bug #2124 fixed embed parent token for simple use cases (fabpot)

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
This commit is contained in:
Fabien Potencier
2016-09-19 16:40:38 -07:00
4 changed files with 83 additions and 3 deletions
+2 -1
View File
@@ -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()
@@ -7,7 +8,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)
+11 -2
View File
@@ -22,18 +22,27 @@ class Twig_TokenParser_Embed extends Twig_TokenParser_Include
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);
@@ -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
@@ -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