function.cycle.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  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. * 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. * @param array $params parameters
  41. * @param Smarty_Internal_Template $template template object
  42. * @return string|null
  43. */
  44. function smarty_function_cycle($params, $template)
  45. {
  46. static $cycle_vars;
  47. $name = (empty($params['name'])) ? 'default' : $params['name'];
  48. $print = (isset($params['print'])) ? (bool)$params['print'] : true;
  49. $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
  50. $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false;
  51. if (!isset($params['values'])) {
  52. if(!isset($cycle_vars[$name]['values'])) {
  53. trigger_error("cycle: missing 'values' parameter");
  54. return;
  55. }
  56. } else {
  57. if(isset($cycle_vars[$name]['values'])
  58. && $cycle_vars[$name]['values'] != $params['values'] ) {
  59. $cycle_vars[$name]['index'] = 0;
  60. }
  61. $cycle_vars[$name]['values'] = $params['values'];
  62. }
  63. if (isset($params['delimiter'])) {
  64. $cycle_vars[$name]['delimiter'] = $params['delimiter'];
  65. } elseif (!isset($cycle_vars[$name]['delimiter'])) {
  66. $cycle_vars[$name]['delimiter'] = ',';
  67. }
  68. if(is_array($cycle_vars[$name]['values'])) {
  69. $cycle_array = $cycle_vars[$name]['values'];
  70. } else {
  71. $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
  72. }
  73. if(!isset($cycle_vars[$name]['index']) || $reset ) {
  74. $cycle_vars[$name]['index'] = 0;
  75. }
  76. if (isset($params['assign'])) {
  77. $print = false;
  78. $template->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
  79. }
  80. if($print) {
  81. $retval = $cycle_array[$cycle_vars[$name]['index']];
  82. } else {
  83. $retval = null;
  84. }
  85. if($advance) {
  86. if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) {
  87. $cycle_vars[$name]['index'] = 0;
  88. } else {
  89. $cycle_vars[$name]['index']++;
  90. }
  91. }
  92. return $retval;
  93. }
  94. ?>