1: <?php
2: /**
3: * Smarty plugin
4: *
5: * @package Smarty
6: * @subpackage PluginsFunction
7: */
8: /**
9: * Smarty {counter} function plugin
10: * Type: function
11: * Name: counter
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('start' => 1, 'skip' => 1, 'direction' => 'up', 'count' => 1);
29: }
30: $counter =& $counters[ $name ];
31: if (isset($params[ 'start' ])) {
32: $counter[ 'start' ] = $counter[ 'count' ] = (int)$params[ 'start' ];
33: }
34: if (!empty($params[ 'assign' ])) {
35: $counter[ 'assign' ] = $params[ 'assign' ];
36: }
37: if (isset($counter[ 'assign' ])) {
38: $template->assign($counter[ 'assign' ], $counter[ 'count' ]);
39: }
40: if (isset($params[ 'print' ])) {
41: $print = (bool)$params[ 'print' ];
42: } else {
43: $print = empty($counter[ 'assign' ]);
44: }
45: if ($print) {
46: $retval = $counter[ 'count' ];
47: } else {
48: $retval = null;
49: }
50: if (isset($params[ 'skip' ])) {
51: $counter[ 'skip' ] = $params[ 'skip' ];
52: }
53: if (isset($params[ 'direction' ])) {
54: $counter[ 'direction' ] = $params[ 'direction' ];
55: }
56: if ($counter[ 'direction' ] === 'down') {
57: $counter[ 'count' ] -= $counter[ 'skip' ];
58: } else {
59: $counter[ 'count' ] += $counter[ 'skip' ];
60: }
61: return $retval;
62: }
63: