From: Fabien Potencier Date: Wed, 29 Dec 2010 10:15:04 +0000 (+0100) Subject: replaced {% parent %} with {{ parent() }} X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=a958ada7d3ca4c5064bfd12b55427fc93a418965;p=web%2Fkonrad%2Ftwig.git replaced {% parent %} with {{ parent() }} --- diff --git a/doc/api.rst b/doc/api.rst index fe69117..43e5bc6 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -270,8 +270,6 @@ The ``core`` extension defines all the core features of Twig: * ``extends`` * ``include`` * ``block`` - * ``parent`` - * ``display`` * ``filter`` * ``macro`` * ``import`` @@ -306,6 +304,7 @@ The ``core`` extension defines all the core features of Twig: * ``range`` * ``constant`` * ``cycle`` + * ``parent`` * Tests: diff --git a/doc/templates.rst b/doc/templates.rst index fc88b27..9f91e8b 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -241,7 +241,7 @@ A child template might look like this: {% block title %}Index{% endblock %} {% block head %} - {% parent %} + {{ parent() }} @@ -293,14 +293,14 @@ Parent Blocks ~~~~~~~~~~~~~ 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 %}

Table Of Contents

... - {% parent %} + {{ parent() }} {% endblock %} Named Block End-Tags diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index c988722..39c5a12 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -213,11 +213,7 @@ class Twig_ExpressionParser } 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; } @@ -231,6 +227,29 @@ class Twig_ExpressionParser 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(); diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index b933344..26ae86e 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_Parent(), new Twig_TokenParser_Display(), new Twig_TokenParser_Filter(), new Twig_TokenParser_Macro(), diff --git a/lib/Twig/Node/Parent.php b/lib/Twig/Node/Expression/Parent.php similarity index 81% rename from lib/Twig/Node/Parent.php rename to lib/Twig/Node/Expression/Parent.php index b2498fa..be07cdb 100644 --- a/lib/Twig/Node/Parent.php +++ b/lib/Twig/Node/Expression/Parent.php @@ -16,7 +16,7 @@ * @package twig * @author Fabien Potencier */ -class Twig_Node_Parent extends Twig_Node +class Twig_Node_Expression_Parent extends Twig_Node_Expression { public function __construct($name, $lineno, $tag = null) { @@ -31,10 +31,9 @@ class Twig_Node_Parent extends Twig_Node public function compile($compiler) { $compiler - ->addDebugInfo($this) - ->write("\$this->displayParentBlock(") + ->raw("\$this->renderParentBlock(") ->string($this->getAttribute('name')) - ->raw(", \$context, \$blocks);\n") + ->raw(", \$context, \$blocks)") ; } } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 46aec99..bf0378e 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -59,6 +59,22 @@ abstract class Twig_Template implements Twig_TemplateInterface } } + 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]); diff --git a/lib/Twig/TokenParser/Parent.php b/lib/Twig/TokenParser/Parent.php deleted file mode 100644 index afde369..0000000 --- a/lib/Twig/TokenParser/Parent.php +++ /dev/null @@ -1,44 +0,0 @@ -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'; - } -} diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/conditional.test b/test/Twig/Tests/Fixtures/tags/inheritance/conditional.test index 3be8c47..8576e77 100644 --- a/test/Twig/Tests/Fixtures/tags/inheritance/conditional.test +++ b/test/Twig/Tests/Fixtures/tags/inheritance/conditional.test @@ -3,7 +3,7 @@ --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)-- diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/multiple.test b/test/Twig/Tests/Fixtures/tags/inheritance/multiple.test index 4aba998..dfc2b6c 100644 --- a/test/Twig/Tests/Fixtures/tags/inheritance/multiple.test +++ b/test/Twig/Tests/Fixtures/tags/inheritance/multiple.test @@ -1,9 +1,9 @@ --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-- diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/parent.test b/test/Twig/Tests/Fixtures/tags/inheritance/parent.test index b4a1daf..4f975db 100644 --- a/test/Twig/Tests/Fixtures/tags/inheritance/parent.test +++ b/test/Twig/Tests/Fixtures/tags/inheritance/parent.test @@ -3,7 +3,7 @@ --TEMPLATE-- {% extends "foo.twig" %} -{% block content %}{% parent %}FOO{% parent %}{% endblock %} +{% block content %}{{ parent() }}FOO{{ parent() }}{% endblock %} --TEMPLATE(foo.twig)-- {% block content %}BAR{% endblock %} --DATA-- diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/parent_nested.test b/test/Twig/Tests/Fixtures/tags/inheritance/parent_nested.test index 1a5cf44..71e7c20 100644 --- a/test/Twig/Tests/Fixtures/tags/inheritance/parent_nested.test +++ b/test/Twig/Tests/Fixtures/tags/inheritance/parent_nested.test @@ -9,7 +9,7 @@ {% endblock %} BEFORE - {% parent %} + {{ parent() }} AFTER {% endblock %} --TEMPLATE(foo.twig)-- @@ -24,4 +24,5 @@ INSIDE OVERRIDDEN BEFORE BAR + AFTER diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/template_instance.test b/test/Twig/Tests/Fixtures/tags/inheritance/template_instance.test index b2c48d9..d1876a5 100644 --- a/test/Twig/Tests/Fixtures/tags/inheritance/template_instance.test +++ b/test/Twig/Tests/Fixtures/tags/inheritance/template_instance.test @@ -4,7 +4,7 @@ {% extends foo %} {% block content %} -{% parent %}FOO +{{ parent() }}FOO {% endblock %} --TEMPLATE(foo.twig)-- {% block content %}BAR{% endblock %} diff --git a/test/Twig/Tests/Node/ParentTest.php b/test/Twig/Tests/Node/Expression/ParentTest.php similarity index 69% rename from test/Twig/Tests/Node/ParentTest.php rename to test/Twig/Tests/Node/Expression/ParentTest.php index 607b4f1..ef18925 100644 --- a/test/Twig/Tests/Node/ParentTest.php +++ b/test/Twig/Tests/Node/Expression/ParentTest.php @@ -9,16 +9,16 @@ * 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')); } @@ -35,7 +35,7 @@ class Twig_Tests_Node_ParentTest extends Twig_Tests_Node_TestCase 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; }