6 * @subpackage PluginsFunction
10 * Smarty {cycle} function plugin
13 * Date: May 3, 2002<br>
14 * Purpose: cycle through given values<br>
17 * - name - name of cycle (optional)
18 * - values - comma separated list of values to cycle, 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 printed.
28 * {cycle values="#eeeeee,#d0d0d0d"}
29 * {cycle name=row values="one,two,three" reset=true}
33 * @link http://www.smarty.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>
41 * @param array $params parameters
42 * @param Smarty_Internal_Template $template template object
47 function smarty_function_cycle($params, $template)
51 $name = (empty($params['name'])) ? 'default' : $params['name'];
52 $print = (isset($params['print'])) ? (bool) $params['print'] : true;
53 $advance = (isset($params['advance'])) ? (bool) $params['advance'] : true;
54 $reset = (isset($params['reset'])) ? (bool) $params['reset'] : false;
56 if (!isset($params['values'])) {
57 if (!isset($cycle_vars[$name]['values'])) {
58 trigger_error("cycle: missing 'values' parameter");
63 if (isset($cycle_vars[$name]['values'])
64 && $cycle_vars[$name]['values'] != $params['values']
66 $cycle_vars[$name]['index'] = 0;
68 $cycle_vars[$name]['values'] = $params['values'];
71 if (isset($params['delimiter'])) {
72 $cycle_vars[$name]['delimiter'] = $params['delimiter'];
73 } elseif (!isset($cycle_vars[$name]['delimiter'])) {
74 $cycle_vars[$name]['delimiter'] = ',';
77 if (is_array($cycle_vars[$name]['values'])) {
78 $cycle_array = $cycle_vars[$name]['values'];
80 $cycle_array = explode($cycle_vars[$name]['delimiter'], $cycle_vars[$name]['values']);
83 if (!isset($cycle_vars[$name]['index']) || $reset) {
84 $cycle_vars[$name]['index'] = 0;
87 if (isset($params['assign'])) {
89 $template->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
93 $retval = $cycle_array[$cycle_vars[$name]['index']];
99 if ($cycle_vars[$name]['index'] >= count($cycle_array) - 1) {
100 $cycle_vars[$name]['index'] = 0;
102 $cycle_vars[$name]['index'] ++;