PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/function.cycle.php

http://zoop.googlecode.com/
PHP | 101 lines | 51 code | 9 blank | 41 comment | 17 complexity | 086f7f65f2506b57f581af4c8f9b03a6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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. *
  15. * Examples:<br>
  16. * <pre>
  17. * {cycle values="#eeeeee,#d0d0d0d"}
  18. * {cycle name=row values="one,two,three" reset=true}
  19. * {cycle name=row}
  20. * </pre>
  21. * @link http://smarty.php.net/manual/en/language.function.cycle.php {cycle}
  22. * (Smarty online manual)
  23. * @author Monte Ohrt <monte at ohrt dot com>
  24. * @author credit to Mark Priatel <mpriatel@rogers.com>
  25. * @author credit to Gerard <gerard@interfold.com>
  26. * @author credit to Jason Sweat <jsweat_php@yahoo.com>
  27. * @param array $params parameters
  28. * Input:
  29. * - name = name of cycle (optional)
  30. * - values = comma separated list of values to cycle,
  31. * or an array of values to cycle
  32. * (this can be left out for subsequent calls)
  33. * - reset = boolean - resets given var to true
  34. * - print = boolean - print var or not. default is true
  35. * - advance = boolean - whether or not to advance the cycle
  36. * - delimiter = the value delimiter, default is ","
  37. * - assign = boolean, assigns to template var instead of
  38. * printed.
  39. * @param object $smarty Smarty object
  40. * @param object $template template object
  41. * @return string|null
  42. */
  43. function smarty_function_cycle($params, $smarty, $template)
  44. {
  45. $name = (empty($params['name'])) ? 'default' : $params['name'];
  46. $print = (isset($params['print'])) ? (bool)$params['print'] : true;
  47. $advance = (isset($params['advance'])) ? (bool)$params['advance'] : true;
  48. $reset = (isset($params['reset'])) ? (bool)$params['reset'] : false;
  49. if (!in_array('values', array_keys($params))) {
  50. if(!isset($template->plugin_data['cycle'][$name]['values'])) {
  51. trigger_error("cycle: missing 'values' parameter",E_USER_WARNING);
  52. return;
  53. }
  54. } else {
  55. if(isset($template->plugin_data['cycle'][$name]['values'])
  56. && $template->plugin_data['cycle'][$name]['values'] != $params['values'] ) {
  57. $template->plugin_data['cycle'][$name]['index'] = 0;
  58. }
  59. $template->plugin_data['cycle'][$name]['values'] = $params['values'];
  60. }
  61. if (isset($params['delimiter'])) {
  62. $template->plugin_data['cycle'][$name]['delimiter'] = $params['delimiter'];
  63. } elseif (!isset($template->plugin_data['cycle'][$name]['delimiter'])) {
  64. $template->plugin_data['cycle'][$name]['delimiter'] = ',';
  65. }
  66. if(is_array($template->plugin_data['cycle'][$name]['values'])) {
  67. $cycle_array = $template->plugin_data['cycle'][$name]['values'];
  68. } else {
  69. $cycle_array = explode($template->plugin_data['cycle'][$name]['delimiter'],$template->plugin_data['cycle'][$name]['values']);
  70. }
  71. if(!isset($template->plugin_data['cycle'][$name]['index']) || $reset ) {
  72. $template->plugin_data['cycle'][$name]['index'] = 0;
  73. }
  74. if (isset($params['assign'])) {
  75. $print = false;
  76. $template->assign($params['assign'], $cycle_array[$template->plugin_data['cycle'][$name]['index']]);
  77. }
  78. if($print) {
  79. $retval = $cycle_array[$template->plugin_data['cycle'][$name]['index']];
  80. } else {
  81. $retval = null;
  82. }
  83. if($advance) {
  84. if ( $template->plugin_data['cycle'][$name]['index'] >= count($cycle_array) -1 ) {
  85. $template->plugin_data['cycle'][$name]['index'] = 0;
  86. } else {
  87. $template->plugin_data['cycle'][$name]['index']++;
  88. }
  89. }
  90. return $retval;
  91. }
  92. ?>