function.counter.php 1.7 KB

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