fixed loop.last when the array only has one item (closes #40)
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Tue, 1 Dec 2009 15:15:03 +0000 (15:15 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Tue, 1 Dec 2009 15:15:03 +0000 (15:15 +0000)
git-svn-id: http://svn.twig-project.org/trunk@148 93ef8e89-cb99-4229-a87c-7fa0fa45744b

CHANGELOG
lib/Twig/Node/For.php

index 6c3f7f3..f114ba1 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -4,6 +4,7 @@ If you have custom loaders, you MUST upgrade them for this release: The
 Twig_Loader base class has been removed, and the Twig_LoaderInterface has also
 been changed (see the source code for more information or the documentation).
 
+ * fixed loop.last when the array only has one item
  * made it possible to insert newlines in tag and variable blocks
  * fixed a bug when a literal '\n' were present in a template text
  * refactored loaders
index fb3f127..cd3e0fc 100644 (file)
@@ -72,17 +72,17 @@ class Twig_Node_For extends Twig_Node implements Twig_NodeListInterface
       ->write("\$seq$var = twig_iterator_to_array(")
       ->subcompile($this->seq)
       ->raw(");\n")
-      ->write("\$context['loop']['length'] = count(\$seq$var);\n")
+      ->write("\$length = count(\$seq$var);\n")
 
       ->write("\$context['loop'] = array(\n")
       ->write("  'parent'    => \$context['_parent'],\n")
-      ->write("  'length'    => \$context['loop']['length'],\n")
+      ->write("  'length'    => \$length,\n")
       ->write("  'index0'    => 0,\n")
       ->write("  'index'     => 1,\n")
-      ->write("  'revindex0' => \$context['loop']['length'] - 1,\n")
-      ->write("  'revindex'  => \$context['loop']['length'],\n")
+      ->write("  'revindex0' => \$length - 1,\n")
+      ->write("  'revindex'  => \$length,\n")
       ->write("  'first'     => true,\n")
-      ->write("  'last'      => false,\n")
+      ->write("  'last'      => 1 === \$length,\n")
       ->write(");\n")
 
       ->write("foreach (\$seq$var as \$context[")