* ``extends``
* ``include``
* ``block``
- * ``parent``
- * ``display``
* ``filter``
* ``macro``
* ``import``
* ``range``
* ``constant``
* ``cycle``
+ * ``parent``
* Tests:
{% block title %}Index{% endblock %}
{% block head %}
- {% parent %}
+ {{ parent() }}
<style type="text/css">
.important { color: #336699; }
</style>
~~~~~~~~~~~~~
It's possible to render the contents of the parent block by using the ``parent``
-tag. This gives back the results of the parent block:
+function. This gives back the results of the parent block:
.. code-block:: jinja
{% block sidebar %}
<h3>Table Of Contents</h3>
...
- {% parent %}
+ {{ parent() }}
{% endblock %}
Named Block End-Tags
} elseif ('|' == $token->getValue()) {
$node = $this->parseFilterExpression($node);
} elseif ($firstPass && $node instanceof Twig_Node_Expression_Name && '(' == $token->getValue()) {
- if (null !== $alias = $this->parser->getImportedFunction($node->getAttribute('name'))) {
- $node = new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $node->getLine()), $this->parseArguments(), $node->getLine(), Twig_Node_Expression_GetAttr::TYPE_METHOD);
- } else {
- $node = new Twig_Node_Expression_Function($node, $this->parseArguments(), $node->getLine());
- }
+ $node = $this->getFunctionNode($node);
} else {
break;
}
return $node;
}
+ public function getFunctionNode($node)
+ {
+ $args = $this->parseArguments();
+
+ if ('parent' === $node->getAttribute('name')) {
+ if (!count($this->parser->getBlockStack())) {
+ throw new Twig_Error_Syntax('Calling "parent" outside a block is forbidden', $token->getLine());
+ }
+
+ if (!$this->parser->getParent()) {
+ throw new Twig_Error_Syntax('Calling "parent" on a template that does not extend another one is forbidden', $token->getLine());
+ }
+
+ return new Twig_Node_Expression_Parent($this->parser->peekBlockStack(), $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);
+ }
+
+ return new Twig_Node_Expression_Function($node, $args, $node->getLine());
+ }
+
public function parseSubscriptExpression($node)
{
$token = $this->parser->getStream()->next();
new Twig_TokenParser_Extends(),
new Twig_TokenParser_Include(),
new Twig_TokenParser_Block(),
- new Twig_TokenParser_Parent(),
new Twig_TokenParser_Display(),
new Twig_TokenParser_Filter(),
new Twig_TokenParser_Macro(),
* @package twig
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
-class Twig_Node_Parent extends Twig_Node
+class Twig_Node_Expression_Parent extends Twig_Node_Expression
{
public function __construct($name, $lineno, $tag = null)
{
public function compile($compiler)
{
$compiler
- ->addDebugInfo($this)
- ->write("\$this->displayParentBlock(")
+ ->raw("\$this->renderParentBlock(")
->string($this->getAttribute('name'))
- ->raw(", \$context, \$blocks);\n")
+ ->raw(", \$context, \$blocks)")
;
}
}
}
}
+ public function renderParentBlock($name, array $context, array $blocks = array())
+ {
+ ob_start();
+ $this->displayParentBlock($name, $context, $blocks);
+
+ return new Twig_Markup(ob_get_clean());
+ }
+
+ public function renderBlock($name, array $context, array $blocks = array())
+ {
+ ob_start();
+ $this->displayBlock($name, $context, $blocks);
+
+ return new Twig_Markup(ob_get_clean());
+ }
+
public function hasBlock($name)
{
return isset($this->blocks[$name]);
+++ /dev/null
-<?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.
- */
-class Twig_TokenParser_Parent 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)
- {
- if (!count($this->parser->getBlockStack())) {
- throw new Twig_Error_Syntax('Calling "parent" outside a block is forbidden', $token->getLine());
- }
- $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
-
- if (!$this->parser->getParent()) {
- throw new Twig_Error_Syntax('Calling "parent" on a template that does not extend another one is forbidden', $token->getLine());
- }
-
- return new Twig_Node_Parent($this->parser->peekBlockStack(), $token->getLine(), $this->getTag());
- }
-
- /**
- * Gets the tag name associated with this token parser.
- *
- * @param string The tag name
- */
- public function getTag()
- {
- return 'parent';
- }
-}
--TEMPLATE--
{% extends standalone ? foo : 'bar.twig' %}
-{% block content %}{% parent %}FOO{% endblock %}
+{% block content %}{{ parent() }}FOO{% endblock %}
--TEMPLATE(foo.twig)--
{% block content %}FOO{% endblock %}
--TEMPLATE(bar.twig)--
--TEST--
"extends" tag
--TEMPLATE--
-{% extends "layout.twig" %}{% block content %}{% parent %}index {% endblock %}
+{% extends "layout.twig" %}{% block content %}{{ parent() }}index {% endblock %}
--TEMPLATE(layout.twig)--
-{% extends "base.twig" %}{% block content %}{% parent %}layout {% endblock %}
+{% extends "base.twig" %}{% block content %}{{ parent() }}layout {% endblock %}
--TEMPLATE(base.twig)--
{% block content %}base {% endblock %}
--DATA--
--TEMPLATE--
{% extends "foo.twig" %}
-{% block content %}{% parent %}FOO{% parent %}{% endblock %}
+{% block content %}{{ parent() }}FOO{{ parent() }}{% endblock %}
--TEMPLATE(foo.twig)--
{% block content %}BAR{% endblock %}
--DATA--
{% endblock %}
BEFORE
- {% parent %}
+ {{ parent() }}
AFTER
{% endblock %}
--TEMPLATE(foo.twig)--
BEFORE
BAR
+
AFTER
{% extends foo %}
{% block content %}
-{% parent %}FOO
+{{ parent() }}FOO
{% endblock %}
--TEMPLATE(foo.twig)--
{% block content %}BAR{% endblock %}
* file that was distributed with this source code.
*/
-require_once dirname(__FILE__).'/TestCase.php';
+require_once dirname(__FILE__).'/../TestCase.php';
-class Twig_Tests_Node_ParentTest extends Twig_Tests_Node_TestCase
+class Twig_Tests_Node_Expression_ParentTest extends Twig_Tests_Node_TestCase
{
/**
* @covers Twig_Node_Parent::__construct
*/
public function testConstructor()
{
- $node = new Twig_Node_Parent('foo', 0);
+ $node = new Twig_Node_Expression_Parent('foo', 0);
$this->assertEquals('foo', $node->getAttribute('name'));
}
public function getTests()
{
$tests = array();
- $tests[] = array(new Twig_Node_Parent('foo', 0), '$this->displayParentBlock("foo", $context, $blocks);');
+ $tests[] = array(new Twig_Node_Expression_Parent('foo', 0), '$this->renderParentBlock("foo", $context, $blocks)');
return $tests;
}