Содержимое файла | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
304 | alex-w | 1 | <?php |
2 | /** |
||
3 | * Smarty Internal Plugin Compile Section |
||
4 | * |
||
5 | * Compiles the {section} tag |
||
6 | * |
||
7 | * @package Smarty |
||
8 | * @subpackage Compiler |
||
9 | * @author Uwe Tews |
||
10 | */ |
||
11 | /** |
||
12 | * Smarty Internal Plugin Compile Section Class |
||
13 | */ |
||
14 | class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase { |
||
15 | /** |
||
16 | * Compiles code for the {section} tag |
||
17 | * |
||
18 | * @param array $args array with attributes from parser |
||
19 | * @param object $compiler compiler object |
||
20 | * @return string compiled code |
||
21 | */ |
||
22 | public function compile($args, $compiler) |
||
23 | { |
||
24 | $this->compiler = $compiler; |
||
25 | $this->required_attributes = array('name', 'loop'); |
||
26 | $this->optional_attributes = array('start', 'step', 'max', 'show'); |
||
27 | // check and get attributes |
||
28 | $_attr = $this->_get_attributes($args); |
||
29 | |||
30 | $this->_open_tag('section'); |
||
31 | |||
32 | $output = "<?php "; |
||
33 | |||
34 | $section_name = $_attr['name']; |
||
35 | |||
36 | $output .= "unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n"; |
||
37 | $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]"; |
||
38 | |||
39 | foreach ($_attr as $attr_name => $attr_value) { |
||
40 | switch ($attr_name) { |
||
41 | case 'loop': |
||
42 | $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n"; |
||
43 | break; |
||
44 | |||
45 | case 'show': |
||
46 | if (is_bool($attr_value)) |
||
47 | $show_attr_value = $attr_value ? 'true' : 'false'; |
||
48 | else |
||
49 | $show_attr_value = "(bool)$attr_value"; |
||
50 | $output .= "{$section_props}['show'] = $show_attr_value;\n"; |
||
51 | break; |
||
52 | |||
53 | case 'name': |
||
54 | $output .= "{$section_props}['$attr_name'] = $attr_value;\n"; |
||
55 | break; |
||
56 | |||
57 | case 'max': |
||
58 | case 'start': |
||
59 | $output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n"; |
||
60 | break; |
||
61 | |||
62 | case 'step': |
||
63 | $output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n"; |
||
64 | break; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | if (!isset($_attr['show'])) |
||
69 | $output .= "{$section_props}['show'] = true;\n"; |
||
70 | |||
71 | if (!isset($_attr['loop'])) |
||
72 | $output .= "{$section_props}['loop'] = 1;\n"; |
||
73 | |||
74 | if (!isset($_attr['max'])) |
||
75 | $output .= "{$section_props}['max'] = {$section_props}['loop'];\n"; |
||
76 | else |
||
77 | $output .= "if ({$section_props}['max'] < 0)\n" . " {$section_props}['max'] = {$section_props}['loop'];\n"; |
||
78 | |||
79 | if (!isset($_attr['step'])) |
||
80 | $output .= "{$section_props}['step'] = 1;\n"; |
||
81 | |||
82 | if (!isset($_attr['start'])) |
||
83 | $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n"; |
||
84 | else { |
||
85 | $output .= "if ({$section_props}['start'] < 0)\n" . " {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" . "else\n" . " {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n"; |
||
86 | } |
||
87 | |||
88 | $output .= "if ({$section_props}['show']) {\n"; |
||
89 | if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) { |
||
90 | $output .= " {$section_props}['total'] = {$section_props}['loop'];\n"; |
||
91 | } else { |
||
92 | $output .= " {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n"; |
||
93 | } |
||
94 | $output .= " if ({$section_props}['total'] == 0)\n" . " {$section_props}['show'] = false;\n" . "} else\n" . " {$section_props}['total'] = 0;\n"; |
||
95 | |||
96 | $output .= "if ({$section_props}['show']):\n"; |
||
97 | $output .= " |
||
98 | for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1; |
||
99 | {$section_props}['iteration'] <= {$section_props}['total']; |
||
100 | {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n"; |
||
101 | $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n"; |
||
102 | $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n"; |
||
103 | $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n"; |
||
104 | $output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n"; |
||
105 | $output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n"; |
||
106 | |||
107 | $output .= "?>"; |
||
108 | return $output; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | ?> |