9 * Author: Monte Ohrt <monte@ispi.net>
10 * Credits: Mark Priatel <mpriatel@rogers.com>
11 * Gerard <gerard@interfold.com>
12 * Jason Sweat <jsweat_php@yahoo.com>
13 * Purpose: cycle through given values
14 * Input: name = name of cycle (optional)
15 * values = comma separated list of values to cycle,
16 * or an array of values to cycle
17 * (this can be left out for subsequent calls)
18 * reset = boolean - resets given var to true
19 * print = boolean - print var or not. default is true
20 * advance = boolean - whether or not to advance the cycle
21 * delimiter = the value delimiter, default is ","
22 * assign = boolean, assigns to template var instead of
24 * Examples: {cycle values="#eeeeee,#d0d0d0d"}
25 * {cycle name=row values="one,two,three" reset=true}
27 * Credit: Taken from the original Smarty
28 * http://smarty.php.net
30 function tpl_function_cycle($params, &$tpl)
34 $name = (empty($params['name'])) ?
'default' : $params['name'];
35 $print = (isset($params['print'])) ?
(bool)$params['print'] : true;
36 $advance = (isset($params['advance'])) ?
(bool)$params['advance'] : true;
37 $reset = (isset($params['reset'])) ?
(bool)$params['reset'] : false;
39 if (!in_array('values', array_keys($params)))
41 if(!isset($cycle_vars[$name]['values']))
43 throw new Template_Exception("cycle: missing 'values' parameter", $tpl);
49 if(isset($cycle_vars[$name]['values']) && $cycle_vars[$name]['values'] != $params['values'] )
51 $cycle_vars[$name]['index'] = 0;
53 $cycle_vars[$name]['values'] = $params['values'];
56 $cycle_vars[$name]['delimiter'] = (isset($params['delimiter'])) ?
$params['delimiter'] : ',';
58 if(is_array($cycle_vars[$name]['values']))
60 $cycle_array = $cycle_vars[$name]['values'];
64 $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
67 if(!isset($cycle_vars[$name]['index']) ||
$reset )
69 $cycle_vars[$name]['index'] = 0;
72 if (isset($params['assign']))
75 $tpl->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
80 $retval = $cycle_array[$cycle_vars[$name]['index']];
89 if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 )
91 $cycle_vars[$name]['index'] = 0;
95 $cycle_vars[$name]['index']++
;