您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

function.counter.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {counter} function plugin
  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. *
  18. * @param array $params parameters
  19. * @param Smarty_Internal_Template $template template object
  20. *
  21. * @return string|null
  22. */
  23. function smarty_function_counter($params, $template)
  24. {
  25. static $counters = array();
  26. $name = (isset($params['name'])) ? $params['name'] : 'default';
  27. if (!isset($counters[$name])) {
  28. $counters[$name] = array(
  29. 'start' => 1,
  30. 'skip' => 1,
  31. 'direction' => 'up',
  32. 'count' => 1
  33. );
  34. }
  35. $counter =& $counters[$name];
  36. if (isset($params['start'])) {
  37. $counter['start'] = $counter['count'] = (int) $params['start'];
  38. }
  39. if (!empty($params['assign'])) {
  40. $counter['assign'] = $params['assign'];
  41. }
  42. if (isset($counter['assign'])) {
  43. $template->assign($counter['assign'], $counter['count']);
  44. }
  45. if (isset($params['print'])) {
  46. $print = (bool) $params['print'];
  47. } else {
  48. $print = empty($counter['assign']);
  49. }
  50. if ($print) {
  51. $retval = $counter['count'];
  52. } else {
  53. $retval = null;
  54. }
  55. if (isset($params['skip'])) {
  56. $counter['skip'] = $params['skip'];
  57. }
  58. if (isset($params['direction'])) {
  59. $counter['direction'] = $params['direction'];
  60. }
  61. if ($counter['direction'] == "down") {
  62. $counter['count'] -= $counter['skip'];
  63. } else {
  64. $counter['count'] += $counter['skip'];
  65. }
  66. return $retval;
  67. }