Automatically exported from code.google.com/p/planningalerts
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

function.cycle.php 3.1 KiB

18 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  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. $cycle_vars[$name]['delimiter'] = (isset($params['delimiter'])) ? $params['delimiter'] : ',';
  63. if(is_array($cycle_vars[$name]['values'])) {
  64. $cycle_array = $cycle_vars[$name]['values'];
  65. } else {
  66. $cycle_array = explode($cycle_vars[$name]['delimiter'],$cycle_vars[$name]['values']);
  67. }
  68. if(!isset($cycle_vars[$name]['index']) || $reset ) {
  69. $cycle_vars[$name]['index'] = 0;
  70. }
  71. if (isset($params['assign'])) {
  72. $print = false;
  73. $smarty->assign($params['assign'], $cycle_array[$cycle_vars[$name]['index']]);
  74. }
  75. if($print) {
  76. $retval = $cycle_array[$cycle_vars[$name]['index']];
  77. } else {
  78. $retval = null;
  79. }
  80. if($advance) {
  81. if ( $cycle_vars[$name]['index'] >= count($cycle_array) -1 ) {
  82. $cycle_vars[$name]['index'] = 0;
  83. } else {
  84. $cycle_vars[$name]['index']++;
  85. }
  86. }
  87. return $retval;
  88. }
  89. /* vim: set expandtab: */
  90. ?>