From: Fabien Potencier Date: Sat, 27 Aug 2011 12:33:01 +0000 (+0200) Subject: added support for an array of templates to the "extends" tag X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=4402ee2c1d8cac3fa9dd165dbfb10a58aeb38b52;p=konrad%2Ftwig.git added support for an array of templates to the "extends" tag --- diff --git a/CHANGELOG b/CHANGELOG index f931328..1162cfa 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,7 @@ * 1.2.0 * added a way to ignore a missing template when using the "include" tag ({% include "foo" ignore missing %}) - * added support for an array of templates to the "include" tag ({% include ['foo', 'bar'] %}) + * added support for an array of templates to the "include" and "extends" tags ({% include ['foo', 'bar'] %}) * added support for bitwise operators in expressions * added the "attribute" function to allow getting dynamic attributes on variables * added Twig_Loader_Chain diff --git a/doc/templates.rst b/doc/templates.rst index b882e4b..de7249a 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -401,6 +401,16 @@ the parent template:: $twig->display('template.twig', array('layout' => $layout)); +.. versionadded:: 1.2 + The possibility to pass an array of templates has been added in Twig 1.2. + +You can also provide a list of templates that are checked for existence. The +first template that exists will be used as a parent:: + +.. code-block:: jinja + + {% extends ['layout.html', 'base_layout.html'] %} + Conditional Inheritance ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 0ac13dd..9b8c55e 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -71,7 +71,15 @@ class Twig_Node_Module extends Twig_Node if (null === $this->getNode('parent')) { $compiler->raw("false"); } else { - $compiler->subcompile($this->getNode('parent')); + if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { + $compiler->subcompile($this->getNode('parent')); + } else { + $compiler + ->raw("\$this->env->resolveTemplate(") + ->subcompile($this->getNode('parent')) + ->raw(")") + ; + } } $compiler diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/extends_as_array.test b/test/Twig/Tests/Fixtures/tags/inheritance/extends_as_array.test new file mode 100644 index 0000000..a1cb1ce --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/inheritance/extends_as_array.test @@ -0,0 +1,12 @@ +--TEST-- +"extends" tag +--TEMPLATE-- +{% extends ["foo.twig", "bar.twig"] %} +--TEMPLATE(bar.twig)-- +{% block content %} +foo +{% endblock %} +--DATA-- +return array() +--EXPECT-- +foo diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 8300919..d88124e 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -149,7 +149,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template { protected function doGetParent(array \$context) { - return ((true) ? ("foo") : ("foo")); + return \$this->env->resolveTemplate(((true) ? ("foo") : ("foo"))); } protected function doDisplay(array \$context, array \$blocks = array())