--- nfo/php/libs/net.php.smarty/plugins/function.counter.php 2002/10/09 00:53:36 1.1 +++ nfo/php/libs/net.php.smarty/plugins/function.counter.php 2004/06/16 21:58:16 1.2 @@ -1,64 +1,77 @@ + * Name: counter
* Purpose: print out a counter value - * ------------------------------------------------------------- + * @link http://smarty.php.net/manual/en/language.function.counter.php {counter} + * (Smarty online manual) + * @param array parameters + * @param Smarty + * @return string|null */ function smarty_function_counter($params, &$smarty) { - static $count = array(); - static $skipval = array(); - static $dir = array(); - static $name = "default"; - static $printval = array(); - static $assign = ""; - - extract($params); - - if (!isset($name)) { - if(isset($id)) { - $name = $id; - } else { - $name = "default"; - } - } - - if (isset($start)) - $count[$name] = $start; - else if (!isset($count[$name])) - $count[$name]=1; + static $counters = array(); - if (!isset($print)) - $printval[$name]=true; - else - $printval[$name]=$print; + $name = (isset($params['name'])) ? $params['name'] : 'default'; + if (!isset($counters[$name])) { + $counters[$name] = array( + 'start'=>1, + 'skip'=>1, + 'direction'=>'up', + 'count'=>1 + ); + } + $counter =& $counters[$name]; + + if (isset($params['start'])) { + $counter['start'] = $counter['count'] = (int)$params['start']; + } + + if (!empty($params['assign'])) { + $counter['assign'] = $params['assign']; + } + + if (isset($counter['assign'])) { + $smarty->assign($counter['assign'], $counter['count']); + } - if (!empty($assign)) { - $printval[$name] = false; - $smarty->assign($assign, $count[$name]); + if (isset($params['print'])) { + $print = (bool)$params['print']; + } else { + $print = empty($counter['assign']); + } + + if ($print) { + $retval = $counter['count']; + } else { + $retval = null; } - if ($printval[$name]) - echo $count[$name]; - - if (isset($skip)) - $skipval[$name] = $skip; - else if (empty($skipval[$name])) - $skipval[$name] = 1; + if (isset($params['skip'])) { + $counter['skip'] = $params['skip']; + } - if (isset($direction)) - $dir[$name] = $direction; - else if (!isset($dir[$name])) - $dir[$name] = "up"; + if (isset($params['direction'])) { + $counter['direction'] = $params['direction']; + } - if ($dir[$name] == "down") - $count[$name] -= $skipval[$name]; + if ($counter['direction'] == "down") + $counter['count'] -= $counter['skip']; else - $count[$name] += $skipval[$name]; + $counter['count'] += $counter['skip']; + + return $retval; + } /* vim: set expandtab: */