replaced {% display foo %} with {{ block('foo') }}

This commit is contained in:
Fabien Potencier
2010-12-29 12:41:28 +01:00
parent a958ada7d3
commit 98edcd0cb6
6 changed files with 46 additions and 42 deletions
+1
View File
@@ -305,6 +305,7 @@ The ``core`` extension defines all the core features of Twig:
* ``constant``
* ``cycle``
* ``parent``
* ``block``
* Tests:
+2 -2
View File
@@ -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
<title>{% block title %}{% endblock %}</title>
<h1>{% display title %}</h1>
<h1>{{ block('title') }}</h1>
{% block body %}{% endblock %}
Like PHP, Twig does not support multiple inheritance. So you can only have one
+4
View File
@@ -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);
}
-1
View File
@@ -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(),
@@ -0,0 +1,39 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Represents a block call node.
*
* @package twig
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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)")
;
}
}
-39
View File
@@ -1,39 +0,0 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_TokenParser_Display extends Twig_TokenParser
{
/**
* Parses a token and returns a node.
*
* @param Twig_Token $token A Twig_Token instance
*
* @return Twig_NodeInterface A Twig_NodeInterface instance
*/
public function parse(Twig_Token $token)
{
$lineno = $token->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';
}
}