Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

function.cycle.php 3.2KB

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