mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
fixed embed parent token for simple use cases
This commit is contained in:
@@ -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()
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in New Issue
Block a user