[prev in list] [next in list] [prev in thread] [next in thread] 

List:       smarty-dev
Subject:    [SMARTY-DEV] {counter} bug and patch
From:       "Bharat Mediratta" <bharat () menalto ! com>
Date:       2003-05-31 5:31:48
[Download RAW message or body]


Hi, all.  I just updated from 2.5.0 to HEAD today to see how
the latest stuff is working and noticed something odd with
{counter}.

I have the following code:

/* PHP */
for ($i = 1; $i < 10; $i++) { 
    $children[] = 'child' . $i; 
} 
$smarty->assign('children', $children); 
$smarty->display('test.tpl'); 

{* Smarty *}
{counter assign=index start=0 print=false} 
{$children[$index]}: {counter} <br> 
{$children[$index]}: {counter} <br> 
{$children[$index]}: {counter} <br> 

Now, on Smarty 2.5.0 the above generates this output:

  child1: 
  child2: 
  child3: 

So far so good.  But in the latest code I see:

 child2: 1 
 child3: 2 
 child4: 3 

Two things are happening.  First, {counter} appears to be 
printing the incremented value.  This is new after Smarty 2.5.0,
but it also jibes with the documentation for counter so I 
guess this was broken in 2.5.0 and is fixed now.

However, it looks like counter is no longer starting at the
"start" number, but is instead starting at "start+skip" which
means that the index into the array that I'm using is now off
by one.  I looked in function.counter.php and found that it 
increments the count every time, including the first time you 
call {counter}.  I created a patch such that if you specify the 
start attribute, it won't increment the count for that invocation.

-Bharat

Index: function.counter.php
===================================================================
RCS file: /repository/smarty/libs/plugins/function.counter.php,v
retrieving revision 1.8
diff -u -w -u -w -r1.8 function.counter.php
--- function.counter.php        2 May 2003 22:33:15 -0000       1.8
+++ function.counter.php        31 May 2003 05:07:52 -0000
@@ -36,9 +36,11 @@
                }
        }
 
-    if (isset($start))
+    $dontIncrement = false;
+    if (isset($start)) {
         $count[$name] = $start;
-    else if (!isset($count[$name]))
+       $dontIncrement = true;
+    } else if (!isset($count[$name]))
         $count[$name]=1;
 
     if (!isset($print))
@@ -67,10 +69,12 @@
     else if (!isset($dir[$name]))
         $dir[$name] = "up";
 
+    if (!$dontIncrement) {
     if ($dir[$name] == "down")
         $count[$name] -= $skipval[$name];
     else
         $count[$name] += $skipval[$name];
+    }
 
        return $retval;
 




-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic