|
1 <?php |
|
2 /** |
|
3 * Smarty plugin |
|
4 * |
|
5 * @package Smarty |
|
6 * @subpackage PluginsFunction |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Smarty {cycle} function plugin |
|
11 * |
|
12 * Type: function<br> |
|
13 * Name: cycle<br> |
|
14 * Date: May 3, 2002<br> |
|
15 * Purpose: cycle through given values<br> |
|
16 * Params: |
|
17 * <pre> |
|
18 * - name - name of cycle (optional) |
|
19 * - values - comma separated list of values to cycle, 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 printed. |
|
26 * </pre> |
|
27 * Examples:<br> |
|
28 * <pre> |
|
29 * {cycle values="#eeeeee,#d0d0d0d"} |
|
30 * {cycle name=row values="one,two,three" reset=true} |
|
31 * {cycle name=row} |
|
32 * </pre> |
|
33 * |
|
34 * @link http://www.smarty.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> |
|
40 * @version 1.3 |
|
41 * @param array $params parameters |
|
42 * @param Smarty_Internal_Template $template template object |
|
43 * @return string|null |
|
44 */ |
|
45 |
|
46 function smarty_function_cycle($params, $template) |
|
47 { |
|
48 static $cycle_vars; |
|
49 |
|
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; |
|
54 |
|
55 if (!isset($params['values'])) { |
|
56 if(!isset($cycle_vars[$name]['values'])) { |
|
57 trigger_error("cycle: missing 'values' parameter"); |
|
58 return; |
|
59 } |
|
60 } else { |
|
61 if(isset($cycle_vars[$name]['values']) |
|
62 && $cycle_vars[$name]['values'] != $params['values'] ) { |
|
63 $cycle_vars[$name]['index'] = 0; |
|
64 } |
|
65 $cycle_vars[$name]['values'] = $params['values']; |
|
66 } |
|
67 |
|
68 if (isset($params['delimiter'])) { |
|
69 $cycle_vars[$name]['delimiter'] = $params['delimiter']; |
|
70 } elseif (!isset($cycle_vars[$name]['delimiter'])) { |
|
71 $cycle_vars[$name]['delimiter'] = ','; |
|
72 } |
|
73 |
|
74 if(is_array($cycle_vars[$name]['values'])) { |
|
75 $cycle_array = $cycle_vars[$name]['values']; |
|
76 } else { |
|
77 $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']); |
|
78 } |
|
79 |
|
80 if(!isset($cycle_vars[$name]['index']) || $reset ) { |
|
81 $cycle_vars[$name]['index'] = 0; |
|
82 } |
|
83 |
|
84 if (isset($params['assign'])) { |
|
85 $print = false; |
|
86 $template->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]); |
|
87 } |
|
88 |
|
89 if($print) { |
|
90 $retval = $cycle_array[$cycle_vars[$name]['index']]; |
|
91 } else { |
|
92 $retval = null; |
|
93 } |
|
94 |
|
95 if($advance) { |
|
96 if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) { |
|
97 $cycle_vars[$name]['index'] = 0; |
|
98 } else { |
|
99 $cycle_vars[$name]['index']++; |
|
100 } |
|
101 } |
|
102 |
|
103 return $retval; |
|
104 } |
|
105 |
|
106 ?> |