function.counter.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage PluginsFunction
  6. */
  7. /**
  8. * Smarty {counter} function plugin
  9. *
  10. * Type: function<br>
  11. * Name: counter<br>
  12. * Purpose: print out a counter value
  13. *
  14. * @author Monte Ohrt <monte at ohrt dot com>
  15. * @link http://www.smarty.net/manual/en/language.function.counter.php {counter}
  16. * (Smarty online manual)
  17. * @param array $params parameters
  18. * @param Smarty_Internal_Template $template template object
  19. * @return string|null
  20. */
  21. function smarty_function_counter($params, $template)
  22. {
  23. static $counters = array();
  24. $name = (isset($params['name'])) ? $params['name'] : 'default';
  25. if (!isset($counters[$name])) {
  26. $counters[$name] = array(
  27. 'start'=>1,
  28. 'skip'=>1,
  29. 'direction'=>'up',
  30. 'count'=>1
  31. );
  32. }
  33. $counter =& $counters[$name];
  34. if (isset($params['start'])) {
  35. $counter['start'] = $counter['count'] = (int)$params['start'];
  36. }
  37. if (!empty($params['assign'])) {
  38. $counter['assign'] = $params['assign'];
  39. }
  40. if (isset($counter['assign'])) {
  41. $template->assign($counter['assign'], $counter['count']);
  42. }
  43. if (isset($params['print'])) {
  44. $print = (bool)$params['print'];
  45. } else {
  46. $print = empty($counter['assign']);
  47. }
  48. if ($print) {
  49. $retval = $counter['count'];
  50. } else {
  51. $retval = null;
  52. }
  53. if (isset($params['skip'])) {
  54. $counter['skip'] = $params['skip'];
  55. }
  56. if (isset($params['direction'])) {
  57. $counter['direction'] = $params['direction'];
  58. }
  59. if ($counter['direction'] == "down")
  60. $counter['count'] -= $counter['skip'];
  61. else
  62. $counter['count'] += $counter['skip'];
  63. return $retval;
  64. }
  65. ?>