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