From: Fabien Potencier Date: Sat, 27 Aug 2011 11:37:56 +0000 (+0200) Subject: added support for an array of templates to the include tag X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=35a8a0d155dd75dc60ecc3b465e8466ef4a49563;p=web%2Fkonrad%2Ftwig.git added support for an array of templates to the include tag --- diff --git a/CHANGELOG b/CHANGELOG index 9c84f11..a533074 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.2.0 + * added support for an array of templates to the "include" tag ({% 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 50541b9..9117dcd 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -877,6 +877,14 @@ directly:: $twig->loadTemplate('template.twig')->display(array('template' => $template)); +.. 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 before +inclusion. The first template that exists will be included:: + + {% include ['page_detailed.html', 'page.html'] %} + Import ~~~~~~ diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 5c302f3..5aa52c8 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -316,6 +316,30 @@ class Twig_Environment return $this->loadedTemplates[$cls] = new $cls($this); } + public function resolveTemplate($names) + { + if (!is_array($names)) { + $names = array($names); + } + + foreach ($names as $name) { + if ($name instanceof Twig_Template) { + return $name; + } + + try { + return $this->loadTemplate($name); + } catch (Exception $e) { + } + } + + if (1 === count($names)) { + throw $e; + } + + throw new Twig_Error_Loader(sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names))); + } + /** * Clears the internal template cache. */ diff --git a/lib/Twig/Node/Include.php b/lib/Twig/Node/Include.php index 33a4007..d740665 100644 --- a/lib/Twig/Node/Include.php +++ b/lib/Twig/Node/Include.php @@ -40,15 +40,9 @@ class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface ; } else { $compiler - ->write("\$template = ") + ->write("\$template = \$this->env->resolveTemplate(") ->subcompile($this->getNode('expr')) - ->raw(";\n") - ->write("if (!\$template") - ->raw(" instanceof Twig_Template) {\n") - ->indent() - ->write("\$template = \$this->env->loadTemplate(\$template);\n") - ->outdent() - ->write("}\n") + ->raw(");\n") ->write('$template->display(') ; } diff --git a/test/Twig/Tests/Fixtures/tags/include/templates_as_array.test b/test/Twig/Tests/Fixtures/tags/include/templates_as_array.test new file mode 100644 index 0000000..ab670ee --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/include/templates_as_array.test @@ -0,0 +1,12 @@ +--TEST-- +"include" tag +--TEMPLATE-- +{% include ["foo.twig", "bar.twig"] %} +{% include ["bar.twig", "foo.twig"] %} +--TEMPLATE(foo.twig)-- +foo +--DATA-- +return array() +--EXPECT-- +foo +foo diff --git a/test/Twig/Tests/Node/IncludeTest.php b/test/Twig/Tests/Node/IncludeTest.php index e174bf3..fd483f9 100644 --- a/test/Twig/Tests/Node/IncludeTest.php +++ b/test/Twig/Tests/Node/IncludeTest.php @@ -56,10 +56,7 @@ class Twig_Tests_Node_IncludeTest extends Twig_Tests_Node_TestCase ); $node = new Twig_Node_Include($expr, null, false, 0); $tests[] = array($node, <<env->loadTemplate(\$template); -} +\$template = \$this->env->resolveTemplate(((true) ? ("foo") : ("foo"))); \$template->display(\$context); EOF );