function.cycle.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage PluginsFunction
  6. */
  7. /**
  8. * Smarty {cycle} function plugin
  9. *
  10. * Type: function<br>
  11. * Name: cycle<br>
  12. * Date: May 3, 2002<br>
  13. * Purpose: cycle through given values<br>
  14. * Input:
  15. * - name = name of cycle (optional)
  16. * - values = comma separated list of values to cycle,
  17. * or an array of values to cycle
  18. * (this can be left out for subsequent calls)
  19. * - reset = boolean - resets given var to true
  20. * - print = boolean - print var or not. default is true
  21. * - advance = boolean - whether or not to advance the cycle
  22. * - delimiter = the value delimiter, default is ","
  23. * - assign = boolean, assigns to template var instead of
  24. * printed.
  25. *
  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. * @link http://smarty.php.net/manual/en/language.function.cycle.php {cycle}
  33. * (Smarty online manual)
  34. * @author Monte Ohrt <monte at ohrt dot com>
  35. * @author credit to Mark Priatel <mpriatel@rogers.com>
  36. * @author credit to Gerard <gerard@interfold.com>
  37. * @author credit to Jason Sweat <jsweat_php@yahoo.com>
  38. * @version 1.3
  39. * @param array
  40. * @param Smarty
  41. * @return string|null
  42. */
  43. function smarty_function_cycle($params, $smarty)
  44. {
  45. static $cycle_vars;
  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. if (!in_array('values', array_keys($params))) {
  51. if(!isset($cycle_vars[$name]['values'])) {
  52. $smarty->trigger_error("cycle: missing 'values' parameter");
  53. return;
  54. }
  55. } else {
  56. if(isset($cycle_vars[$name]['values'])
  57. && $cycle_vars[$name]['values'] != $params['values'] ) {
  58. $cycle_vars[$name]['index'] = 0;
  59. }
  60. $cycle_vars[$name]['values'] = $params['values'];
  61. }
  62. if (isset($params['delimiter'])) {
  63. $cycle_vars[$name]['delimiter'] = $params['delimiter'];
  64. } elseif (!isset($cycle_vars[$name]['delimiter'])) {
  65. $cycle_vars[$name]['delimiter'] = ',';
  66. }
  67. if(is_array($cycle_vars[$name]['values'])) {
  68. $cycle_array = $cycle_vars[$name]['values'];
  69. } else {
  70. $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
  71. }
  72. if(!isset($cycle_vars[$name]['index']) || $reset ) {
  73. $cycle_vars[$name]['index'] = 0;
  74. }
  75. if (isset($params['assign'])) {
  76. $print = false;
  77. $smarty->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
  78. }
  79. if($print) {
  80. $retval = $cycle_array[$cycle_vars[$name]['index']];
  81. } else {
  82. $retval = null;
  83. }
  84. if($advance) {
  85. if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) {
  86. $cycle_vars[$name]['index'] = 0;
  87. } else {
  88. $cycle_vars[$name]['index']++;
  89. }
  90. }
  91. return $retval;
  92. }
  93. ?>