added the parent as an argument to the embed tag (removes the need to use extends)

This commit is contained in:
Fabien Potencier
2012-04-24 08:31:52 +02:00
parent fab21d8a76
commit 0f623a1112
5 changed files with 31 additions and 19 deletions
+6 -11
View File
@@ -9,9 +9,7 @@ from an external file (like with the ``include`` statement):
.. code-block:: jinja .. code-block:: jinja
{% embed %} {% embed "sidebar.twig" %}
{% extends "sidebar.twig" %}
{% block content %} {% block content %}
Some content for the sidebar Some content for the sidebar
{% endblock %} {% endblock %}
@@ -93,9 +91,7 @@ content is kept in each page (as in solution 2):
{% extends page %} {% extends page %}
{% block base %} {% block base %}
{% embed %} {% embed "base_A.twig" %}
{% extends "base_A.twig" %}
{% block content1 %} {% block content1 %}
Content 1 for page 2 Content 1 for page 2
{% endblock %} {% endblock %}
@@ -127,20 +123,19 @@ And here is the code for ``base_A.twig``:
The goal of the ``base_a.twig`` base template being to factor out the ``Some The goal of the ``base_a.twig`` base template being to factor out the ``Some
code``, ``Some other code``, and ``Yet some other code`` parts. code``, ``Some other code``, and ``Yet some other code`` parts.
The ``embed`` tag can be customized with the same options (``with``, ``only``, The ``embed`` tag takes the exact same arguments as the ``include`` tag:
``ignore missing``) as the ``include`` tag:
.. code-block:: jinja .. code-block:: jinja
{% embed with {'foo': 'bar'} %} {% embed "base" with {'foo': 'bar'} %}
... ...
{% endembed %} {% endembed %}
{% embed with {'foo': 'bar'} only %} {% embed "base" with {'foo': 'bar'} only %}
... ...
{% endembed %} {% endembed %}
{% embed ignore missing %} {% embed "base" ignore missing %}
... ...
{% endembed %} {% endembed %}
+18 -2
View File
@@ -23,12 +23,28 @@ class Twig_TokenParser_Embed extends Twig_TokenParser_Include
*/ */
public function parse(Twig_Token $token) public function parse(Twig_Token $token)
{ {
$stream = $this->parser->getStream();
$parent = $this->parser->getExpressionParser()->parseExpression();
list($variables, $only, $ignoreMissing) = $this->parseArguments(); list($variables, $only, $ignoreMissing) = $this->parseArguments();
$module = $this->parser->parse($this->parser->getStream(), array($this, 'decideBlockEnd'), true); // 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()),
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);
$this->parser->embedTemplate($module); $this->parser->embedTemplate($module);
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); $stream->expect(Twig_Token::BLOCK_END_TYPE);
return new Twig_Node_Embed($module->getAttribute('filename'), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag()); return new Twig_Node_Embed($module->getAttribute('filename'), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
} }
+5
View File
@@ -45,6 +45,11 @@ class Twig_TokenStream
return implode("\n", $this->tokens); return implode("\n", $this->tokens);
} }
public function injectTokens(array $tokens)
{
$this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current));
}
/** /**
* Sets the pointer to the next token and returns the old one. * Sets the pointer to the next token and returns the old one.
* *
@@ -2,9 +2,7 @@
"embed" tag "embed" tag
--TEMPLATE-- --TEMPLATE--
FOO FOO
{% embed %} {% embed "foo.twig" %}
{% extends "foo.twig" %}
{% block c1 %} {% block c1 %}
{{ parent() }} {{ parent() }}
block1extended block1extended
@@ -9,9 +9,7 @@
{% endblock %} {% endblock %}
{% block c2 %} {% block c2 %}
{% embed %} {% embed "foo.twig" %}
{% extends "foo.twig" %}
{% block c1 %} {% block c1 %}
{{ parent() }} {{ parent() }}
block1extended block1extended