From 98edcd0cb630fae99cdef247c7f59add2b24743d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 29 Dec 2010 12:41:28 +0100 Subject: [PATCH] replaced {% display foo %} with {{ block('foo') }} --- doc/api.rst | 1 + doc/templates.rst | 4 +- lib/Twig/ExpressionParser.php | 4 +++ lib/Twig/Extension/Core.php | 1 - lib/Twig/Node/Expression/BlockReference.php | 39 +++++++++++++++++++++++++++ lib/Twig/TokenParser/Display.php | 39 --------------------------- 6 files changed, 46 insertions(+), 42 deletions(-) create mode 100644 lib/Twig/Node/Expression/BlockReference.php delete mode 100644 lib/Twig/TokenParser/Display.php diff --git a/doc/api.rst b/doc/api.rst index 43e5bc6..3abc46c 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -305,6 +305,7 @@ The ``core`` extension defines all the core features of Twig: * ``constant`` * ``cycle`` * ``parent`` + * ``block`` * Tests: diff --git a/doc/templates.rst b/doc/templates.rst index 9f91e8b..af173b4 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -278,12 +278,12 @@ similarly-named ``{% block %}`` tags in a template, that template's parent wouldn't know which one of the blocks' content to use. If you want to print a block multiple times you can however use the -``display`` tag: +``block`` function: .. code-block:: jinja {% block title %}{% endblock %} -

{% display title %}

+

{{ block('title') }}

{% block body %}{% endblock %} Like PHP, Twig does not support multiple inheritance. So you can only have one diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 39c5a12..6fe0634 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -243,6 +243,10 @@ class Twig_ExpressionParser return new Twig_Node_Expression_Parent($this->parser->peekBlockStack(), $node->getLine()); } + if ('block' === $node->getAttribute('name')) { + return new Twig_Node_Expression_BlockReference($args->getNode(0), $node->getLine()); + } + if (null !== $alias = $this->parser->getImportedFunction($node->getAttribute('name'))) { return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $node->getLine()), $args, $node->getLine(), Twig_Node_Expression_GetAttr::TYPE_METHOD); } diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 26ae86e..e63e6a7 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -23,7 +23,6 @@ class Twig_Extension_Core extends Twig_Extension new Twig_TokenParser_Extends(), new Twig_TokenParser_Include(), new Twig_TokenParser_Block(), - new Twig_TokenParser_Display(), new Twig_TokenParser_Filter(), new Twig_TokenParser_Macro(), new Twig_TokenParser_Import(), diff --git a/lib/Twig/Node/Expression/BlockReference.php b/lib/Twig/Node/Expression/BlockReference.php new file mode 100644 index 0000000..2dea781 --- /dev/null +++ b/lib/Twig/Node/Expression/BlockReference.php @@ -0,0 +1,39 @@ + + */ +class Twig_Node_Expression_BlockReference extends Twig_Node_Expression +{ + public function __construct(Twig_NodeInterface $name, $lineno, $tag = null) + { + parent::__construct(array('name' => $name), array(), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param Twig_Compiler A Twig_Compiler instance + */ + public function compile($compiler) + { + $compiler + ->raw("\$this->renderBlock(") + ->subcompile($this->getNode('name')) + ->raw(", \$context, \$blocks)") + ; + } +} diff --git a/lib/Twig/TokenParser/Display.php b/lib/Twig/TokenParser/Display.php deleted file mode 100644 index e1c2083..0000000 --- a/lib/Twig/TokenParser/Display.php +++ /dev/null @@ -1,39 +0,0 @@ -getLine(); - $name = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue(); - - $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); - - return new Twig_Node_BlockReference($name, $lineno, $this->getTag()); - } - - /** - * Gets the tag name associated with this token parser. - * - * @param string The tag name - */ - public function getTag() - { - return 'display'; - } -} -- 1.7.2.5