made a small optimization to for loop when no else clause exists
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Mon, 12 Oct 2009 12:12:29 +0000 (12:12 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Mon, 12 Oct 2009 12:12:29 +0000 (12:12 +0000)
git-svn-id: http://svn.twig-project.org/trunk@32 93ef8e89-cb99-4229-a87c-7fa0fa45744b

lib/Twig/Node/For.php

index 5c2449e..e6d8482 100644 (file)
@@ -51,16 +51,28 @@ class Twig_Node_For extends Twig_Node implements Twig_NodeListInterface
     $compiler
       ->addDebugInfo($this)
       ->pushContext()
-      ->write("\$context['_iterated'] = false;\n")
+    ;
+
+    if (!is_null($this->else))
+    {
+      $compiler->write("\$context['_iterated'] = false;\n");
+    }
+
+    $compiler
       ->write('foreach (twig_iterate($context, ')
       ->subcompile($this->seq)
       ->raw(") as \$iterator)\n")
       ->write("{\n")
       ->indent()
-      ->write("\$context['_iterated'] = true;\n")
-      ->write('twig_set_loop_context($context, $iterator, ');
     ;
 
+    if (!is_null($this->else))
+    {
+      $compiler->write("\$context['_iterated'] = true;\n");
+    }
+
+    $compiler->write('twig_set_loop_context($context, $iterator, ');
+
     if ($this->isMultitarget)
     {
       $compiler->raw('array(');