added support for an array of templates to the include tag
authorFabien Potencier <fabien.potencier@gmail.com>
Sat, 27 Aug 2011 11:37:56 +0000 (13:37 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Sat, 27 Aug 2011 11:58:36 +0000 (13:58 +0200)
CHANGELOG
doc/templates.rst
lib/Twig/Environment.php
lib/Twig/Node/Include.php
test/Twig/Tests/Fixtures/tags/include/templates_as_array.test [new file with mode: 0644]
test/Twig/Tests/Node/IncludeTest.php

index 9c84f11..a533074 100644 (file)
--- 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
index 50541b9..9117dcd 100644 (file)
@@ -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
 ~~~~~~
 
index 5c302f3..5aa52c8 100644 (file)
@@ -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.
      */
index 33a4007..d740665 100644 (file)
@@ -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 (file)
index 0000000..ab670ee
--- /dev/null
@@ -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
index e174bf3..fd483f9 100644 (file)
@@ -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, <<<EOF
-\$template = ((true) ? ("foo") : ("foo"));
-if (!\$template instanceof Twig_Template) {
-    \$template = \$this->env->loadTemplate(\$template);
-}
+\$template = \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
 \$template->display(\$context);
 EOF
         );