9 * Smarty {cycle} function plugin
13 * Date: May 3, 2002<br>
14 * Purpose: cycle through given values<br>
16 * - name = name of cycle (optional)
17 * - values = comma separated list of values to cycle,
18 * or an array of values to cycle
19 * (this can be left out for subsequent calls)
20 * - reset = boolean - resets given var to true
21 * - print = boolean - print var or not. default is true
22 * - advance = boolean - whether or not to advance the cycle
23 * - delimiter = the value delimiter, default is ","
24 * - assign = boolean, assigns to template var instead of
29 * {cycle values="#eeeeee,#d0d0d0d"}
30 * {cycle name=row values="one,two,three" reset=true}
33 * @link http://smarty.php.net/manual/en/language.function.cycle.php {cycle}
34 * (Smarty online manual)
35 * @author Monte Ohrt <monte at ohrt dot com>
36 * @author credit to Mark Priatel <mpriatel@rogers.com>
37 * @author credit to Gerard <gerard@interfold.com>
38 * @author credit to Jason Sweat <jsweat_php@yahoo.com>
44 function smarty_function_cycle($params, &$smarty)
48 $name = (empty($params['name'])) ?
'default' : $params['name'];
49 $print = (isset($params['print'])) ?
(bool)$params['print'] : true;
50 $advance = (isset($params['advance'])) ?
(bool)$params['advance'] : true;
51 $reset = (isset($params['reset'])) ?
(bool)$params['reset'] : false;
53 if (!in_array('values', array_keys($params))) {
54 if(!isset($cycle_vars[$name]['values'])) {
55 $smarty->trigger_error("cycle: missing 'values' parameter");
59 if(isset($cycle_vars[$name]['values'])
60 && $cycle_vars[$name]['values'] != $params['values'] ) {
61 $cycle_vars[$name]['index'] = 0;
63 $cycle_vars[$name]['values'] = $params['values'];
66 $cycle_vars[$name]['delimiter'] = (isset($params['delimiter'])) ?
$params['delimiter'] : ',';
68 if(is_array($cycle_vars[$name]['values'])) {
69 $cycle_array = $cycle_vars[$name]['values'];
71 $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
74 if(!isset($cycle_vars[$name]['index']) ||
$reset ) {
75 $cycle_vars[$name]['index'] = 0;
78 if (isset($params['assign'])) {
80 $smarty->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
84 $retval = $cycle_array[$cycle_vars[$name]['index']];
90 if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) {
91 $cycle_vars[$name]['index'] = 0;
93 $cycle_vars[$name]['index']++
;
100 /* vim: set expandtab: */