From a59e183f73f719920b038d8992b9ecb0c4c33cd7 Mon Sep 17 00:00:00 2001 From: fabien Date: Tue, 1 Dec 2009 15:15:03 +0000 Subject: [PATCH] fixed loop.last when the array only has one item (closes #40) git-svn-id: http://svn.twig-project.org/trunk@148 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- CHANGELOG | 1 + lib/Twig/Node/For.php | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 6c3f7f3..f114ba1 100644 --- 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 diff --git a/lib/Twig/Node/For.php b/lib/Twig/Node/For.php index fb3f127..cd3e0fc 100644 --- a/lib/Twig/Node/For.php +++ b/lib/Twig/Node/For.php @@ -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[") -- 1.7.2.5