fixed embed parent token for simple use cases

This commit is contained in:
Fabien Potencier
2016-09-19 16:20:41 -07:00
parent a9082dd29f
commit a0489854ba
4 changed files with 82 additions and 2 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.25.0 (2016-XX-XX) * 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 the ability to store non Node instances in Node::$nodes
* deprecated Twig_Environment::getLexer(), Twig_Environment::getParser(), Twig_Environment::getCompiler() * deprecated Twig_Environment::getLexer(), Twig_Environment::getParser(), Twig_Environment::getCompiler()
* deprecated Twig_Compiler::getFilename() * deprecated Twig_Compiler::getFilename()
+11 -2
View File
@@ -22,18 +22,27 @@ class Twig_TokenParser_Embed extends Twig_TokenParser_Include
list($variables, $only, $ignoreMissing) = $this->parseArguments(); 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 // inject a fake parent to make the parent() function work
$stream->injectTokens(array( $stream->injectTokens(array(
new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', $token->getLine()), 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::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()), new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', $token->getLine()),
)); ));
$module = $this->parser->parse($stream, array($this, 'decideBlockEnd'), true); $module = $this->parser->parse($stream, array($this, 'decideBlockEnd'), true);
// override the parent with the correct one // override the parent with the correct one
$module->setNode('parent', $parent); if ($fakeParentToken === $parentToken) {
$module->setNode('parent', $parent);
}
$this->parser->embedTemplate($module); $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