6 * @subpackage PluginsFunction
10 * Smarty {cycle} function plugin
14 * Date: May 3, 2002<br>
15 * Purpose: cycle through given values<br>
17 * - name = name of cycle (optional)
18 * - values = comma separated list of values to cycle,
19 * or an array of values to cycle
20 * (this can be left out for subsequent calls)
21 * - reset = boolean - resets given var to true
22 * - print = boolean - print var or not. default is true
23 * - advance = boolean - whether or not to advance the cycle
24 * - delimiter = the value delimiter, default is ","
25 * - assign = boolean, assigns to template var instead of
30 * {cycle values="#eeeeee,#d0d0d0d"}
31 * {cycle name=row values="one,two,three" reset=true}
34 * @link http://smarty.php.net/manual/en/language.function.cycle.php {cycle}
35 * (Smarty online manual)
36 * @author Monte Ohrt <monte at ohrt dot com>
37 * @author credit to Mark Priatel <mpriatel@rogers.com>
38 * @author credit to Gerard <gerard@interfold.com>
39 * @author credit to Jason Sweat <jsweat_php@yahoo.com>
42 * @param object $template template object
46 function smarty_function_cycle($params, $template)
50 $name = (empty($params['name'])) ? 'default' : $params['name'];
51 $print = (isset($params['print'])) ? (bool)$params['print'] : true;
52 $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
53 $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false;
55 if (!in_array('values', array_keys($params))) {
56 if(!isset($cycle_vars[$name]['values'])) {
57 trigger_error("cycle: missing 'values' parameter");
61 if(isset($cycle_vars[$name]['values'])
62 && $cycle_vars[$name]['values'] != $params['values'] ) {
63 $cycle_vars[$name]['index'] = 0;
65 $cycle_vars[$name]['values'] = $params['values'];
68 if (isset($params['delimiter'])) {
69 $cycle_vars[$name]['delimiter'] = $params['delimiter'];
70 } elseif (!isset($cycle_vars[$name]['delimiter'])) {
71 $cycle_vars[$name]['delimiter'] = ',';
74 if(is_array($cycle_vars[$name]['values'])) {
75 $cycle_array = $cycle_vars[$name]['values'];
77 $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
80 if(!isset($cycle_vars[$name]['index']) || $reset ) {
81 $cycle_vars[$name]['index'] = 0;
84 if (isset($params['assign'])) {
86 $template->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
90 $retval = $cycle_array[$cycle_vars[$name]['index']];
96 if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) {
97 $cycle_vars[$name]['index'] = 0;
99 $cycle_vars[$name]['index']++;