Хранилища Subversion ant

Редакция

Содержимое файла | Последнее изменение | Открыть журнал | RSS

Редакция Автор № строки Строка
304 alex-w 1
<?php
2
/**
3
 * Smarty plugin
4
 * @package Smarty
5
 * @subpackage PluginsFunction
6
 */
7
 
8
/**
9
 * Smarty {cycle} function plugin
10
 *
11
 * Type:     function<br>
12
 * Name:     cycle<br>
13
 * Date:     May 3, 2002<br>
14
 * Purpose:  cycle through given values<br>
15
 *
16
 * Examples:<br>
17
 * <pre>
18
 * {cycle values="#eeeeee,#d0d0d0d"}
19
 * {cycle name=row values="one,two,three" reset=true}
20
 * {cycle name=row}
21
 * </pre>
22
 * @link http://smarty.php.net/manual/en/language.function.cycle.php {cycle}
23
 *       (Smarty online manual)
24
 * @author Monte Ohrt <monte at ohrt dot com>
25
 * @author credit to Mark Priatel <mpriatel@rogers.com>
26
 * @author credit to Gerard <gerard@interfold.com>
27
 * @author credit to Jason Sweat <jsweat_php@yahoo.com>
28
 * @param array $params parameters
29
 * Input:
30
 *         - name = name of cycle (optional)
31
 *         - values = comma separated list of values to cycle,
32
 *                    or an array of values to cycle
33
 *                    (this can be left out for subsequent calls)
34
 *         - reset = boolean - resets given var to true
35
 *         - print = boolean - print var or not. default is true
36
 *         - advance = boolean - whether or not to advance the cycle
37
 *         - delimiter = the value delimiter, default is ","
38
 *         - assign = boolean, assigns to template var instead of
39
 *                    printed.
40
 * @param object $smarty Smarty object
41
 * @param object $template template object
42
 * @return string|null
43
 */
44
function smarty_function_cycle($params, $smarty, $template)
45
{
46
    $name = (empty($params['name'])) ? 'default' : $params['name'];
47
    $print = (isset($params['print'])) ? (bool)$params['print'] : true;
48
    $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
49
    $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false;
50
 
51
    if (!in_array('values', array_keys($params))) {
52
        if(!isset($template->plugin_data['cycle'][$name]['values'])) {
53
            throw new Exception ("cycle: missing 'values' parameter");
54
            return;
55
        }
56
    } else {
57
        if(isset($template->plugin_data['cycle'][$name]['values'])
58
            && $template->plugin_data['cycle'][$name]['values'] != $params['values'] ) {
59
            $template->plugin_data['cycle'][$name]['index'] = 0;
60
        }
61
        $template->plugin_data['cycle'][$name]['values'] = $params['values'];
62
    }
63
 
64
    $template->plugin_data['cycle'][$name]['delimiter'] = (isset($params['delimiter'])) ? $params['delimiter'] : ',';
65
 
66
    if(is_array($template->plugin_data['cycle'][$name]['values'])) {
67
        $cycle_array = $template->plugin_data['cycle'][$name]['values'];
68
    } else {
69
        $cycle_array = explode($template->plugin_data['cycle'][$name]['delimiter'],$template->plugin_data['cycle'][$name]['values']);
70
    }
71
 
72
    if(!isset($template->plugin_data['cycle'][$name]['index']) || $reset ) {
73
        $template->plugin_data['cycle'][$name]['index'] = 0;
74
    }
75
 
76
    if (isset($params['assign'])) {
77
        $print = false;
78
        $template->assign($params['assign'], $cycle_array[$template->plugin_data['cycle'][$name]['index']]);
79
    }
80
 
81
    if($print) {
82
        $retval = $cycle_array[$template->plugin_data['cycle'][$name]['index']];
83
    } else {
84
        $retval = null;
85
    }
86
 
87
    if($advance) {
88
        if ( $template->plugin_data['cycle'][$name]['index'] >= count($cycle_array) -1 ) {
89
            $template->plugin_data['cycle'][$name]['index'] = 0;
90
        } else {
91
            $template->plugin_data['cycle'][$name]['index']++;
92
        }
93
    }
94
 
95
    return $retval;
96
}
97
?>