From a958ada7d3ca4c5064bfd12b55427fc93a418965 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 29 Dec 2010 11:15:04 +0100 Subject: [PATCH] replaced {% parent %} with {{ parent() }} --- doc/api.rst | 3 +- doc/templates.rst | 6 +- lib/Twig/ExpressionParser.php | 29 +++++++++++-- lib/Twig/Extension/Core.php | 1 - lib/Twig/Node/Expression/Parent.php | 39 +++++++++++++++++ lib/Twig/Node/Parent.php | 40 ------------------ lib/Twig/Template.php | 16 +++++++ lib/Twig/TokenParser/Parent.php | 44 -------------------- .../Fixtures/tags/inheritance/conditional.test | 2 +- .../Tests/Fixtures/tags/inheritance/multiple.test | 4 +- .../Tests/Fixtures/tags/inheritance/parent.test | 2 +- .../Fixtures/tags/inheritance/parent_nested.test | 3 +- .../tags/inheritance/template_instance.test | 2 +- test/Twig/Tests/Node/Expression/ParentTest.php | 42 +++++++++++++++++++ test/Twig/Tests/Node/ParentTest.php | 42 ------------------- 15 files changed, 132 insertions(+), 143 deletions(-) create mode 100644 lib/Twig/Node/Expression/Parent.php delete mode 100644 lib/Twig/Node/Parent.php delete mode 100644 lib/Twig/TokenParser/Parent.php create mode 100644 test/Twig/Tests/Node/Expression/ParentTest.php delete mode 100644 test/Twig/Tests/Node/ParentTest.php 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/Expression/Parent.php b/lib/Twig/Node/Expression/Parent.php new file mode 100644 index 0000000..be07cdb --- /dev/null +++ b/lib/Twig/Node/Expression/Parent.php @@ -0,0 +1,39 @@ + + */ +class Twig_Node_Expression_Parent extends Twig_Node_Expression +{ + public function __construct($name, $lineno, $tag = null) + { + parent::__construct(array(), array('name' => $name), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param Twig_Compiler A Twig_Compiler instance + */ + public function compile($compiler) + { + $compiler + ->raw("\$this->renderParentBlock(") + ->string($this->getAttribute('name')) + ->raw(", \$context, \$blocks)") + ; + } +} diff --git a/lib/Twig/Node/Parent.php b/lib/Twig/Node/Parent.php deleted file mode 100644 index b2498fa..0000000 --- a/lib/Twig/Node/Parent.php +++ /dev/null @@ -1,40 +0,0 @@ - - */ -class Twig_Node_Parent extends Twig_Node -{ - public function __construct($name, $lineno, $tag = null) - { - parent::__construct(array(), array('name' => $name), $lineno, $tag); - } - - /** - * Compiles the node to PHP. - * - * @param Twig_Compiler A Twig_Compiler instance - */ - public function compile($compiler) - { - $compiler - ->addDebugInfo($this) - ->write("\$this->displayParentBlock(") - ->string($this->getAttribute('name')) - ->raw(", \$context, \$blocks);\n") - ; - } -} 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/Expression/ParentTest.php b/test/Twig/Tests/Node/Expression/ParentTest.php new file mode 100644 index 0000000..ef18925 --- /dev/null +++ b/test/Twig/Tests/Node/Expression/ParentTest.php @@ -0,0 +1,42 @@ +assertEquals('foo', $node->getAttribute('name')); + } + + /** + * @covers Twig_Node_Parent::compile + * @dataProvider getTests + */ + public function testCompile($node, $source, $environment = null) + { + parent::testCompile($node, $source, $environment); + } + + public function getTests() + { + $tests = array(); + $tests[] = array(new Twig_Node_Expression_Parent('foo', 0), '$this->renderParentBlock("foo", $context, $blocks)'); + + return $tests; + } +} diff --git a/test/Twig/Tests/Node/ParentTest.php b/test/Twig/Tests/Node/ParentTest.php deleted file mode 100644 index 607b4f1..0000000 --- a/test/Twig/Tests/Node/ParentTest.php +++ /dev/null @@ -1,42 +0,0 @@ -assertEquals('foo', $node->getAttribute('name')); - } - - /** - * @covers Twig_Node_Parent::compile - * @dataProvider getTests - */ - public function testCompile($node, $source, $environment = null) - { - parent::testCompile($node, $source, $environment); - } - - public function getTests() - { - $tests = array(); - $tests[] = array(new Twig_Node_Parent('foo', 0), '$this->displayParentBlock("foo", $context, $blocks);'); - - return $tests; - } -} -- 1.7.2.5