1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
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: | |