|
1 <?php |
|
2 /** |
|
3 * Smarty Internal Plugin Compile For |
|
4 * Compiles the {for} {forelse} {/for} tags |
|
5 * |
|
6 * @package Smarty |
|
7 * @subpackage Compiler |
|
8 * @author Uwe Tews |
|
9 */ |
|
10 |
|
11 /** |
|
12 * Smarty Internal Plugin Compile For Class |
|
13 * |
|
14 * @package Smarty |
|
15 * @subpackage Compiler |
|
16 */ |
|
17 class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase { |
|
18 /** |
|
19 * Compiles code for the {for} tag |
|
20 * Smarty 3 does implement two different syntax's: |
|
21 * - {for $var in $array} |
|
22 * For looping over arrays or iterators |
|
23 * - {for $x=0; $x<$y; $x++} |
|
24 * For general loops |
|
25 * The parser is generating different sets of attribute by which this compiler can |
|
26 * determine which syntax is used. |
|
27 * |
|
28 * @param array $args array with attributes from parser |
|
29 * @param object $compiler compiler object |
|
30 * @param array $parameter array with compilation parameter |
|
31 * |
|
32 * @return string compiled code |
|
33 */ |
|
34 public function compile($args, $compiler, $parameter) { |
|
35 if ($parameter == 0) { |
|
36 $this->required_attributes = array('start', 'to'); |
|
37 $this->optional_attributes = array('max', 'step'); |
|
38 } else { |
|
39 $this->required_attributes = array('start', 'ifexp', 'var', 'step'); |
|
40 $this->optional_attributes = array(); |
|
41 } |
|
42 // check and get attributes |
|
43 $_attr = $this->getAttributes($compiler, $args); |
|
44 |
|
45 $output = "<?php "; |
|
46 if ($parameter == 1) { |
|
47 foreach ($_attr['start'] as $_statement) { |
|
48 if (is_array($_statement['var'])) { |
|
49 $var = $_statement['var']['var']; |
|
50 $index = $_statement['var']['smarty_internal_index']; |
|
51 } else { |
|
52 $var = $_statement['var']; |
|
53 $index = ''; |
|
54 } |
|
55 $output .= " \$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable;"; |
|
56 $output .= " \$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value];\n"; |
|
57 } |
|
58 if (is_array($_attr['var'])) { |
|
59 $var = $_attr['var']['var']; |
|
60 $index = $_attr['var']['smarty_internal_index']; |
|
61 } else { |
|
62 $var = $_attr['var']; |
|
63 $index = ''; |
|
64 } |
|
65 $output .= " if ($_attr[ifexp]) { for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n"; |
|
66 } else { |
|
67 $_statement = $_attr['start']; |
|
68 if (is_array($_statement['var'])) { |
|
69 $var = $_statement['var']['var']; |
|
70 $index = $_statement['var']['smarty_internal_index']; |
|
71 } else { |
|
72 $var = $_statement['var']; |
|
73 $index = ''; |
|
74 } |
|
75 $output .= "\$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable;"; |
|
76 if (isset($_attr['step'])) { |
|
77 $output .= "\$_smarty_tpl->tpl_vars[$var]->step = $_attr[step];"; |
|
78 } else { |
|
79 $output .= "\$_smarty_tpl->tpl_vars[$var]->step = 1;"; |
|
80 } |
|
81 if (isset($_attr['max'])) { |
|
82 $output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) min(ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step)),$_attr[max]);\n"; |
|
83 } else { |
|
84 $output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step));\n"; |
|
85 } |
|
86 $output .= "if (\$_smarty_tpl->tpl_vars[$var]->total > 0) {\n"; |
|
87 $output .= "for (\$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value], \$_smarty_tpl->tpl_vars[$var]->iteration = 1;\$_smarty_tpl->tpl_vars[$var]->iteration <= \$_smarty_tpl->tpl_vars[$var]->total;\$_smarty_tpl->tpl_vars[$var]->value{$index} += \$_smarty_tpl->tpl_vars[$var]->step, \$_smarty_tpl->tpl_vars[$var]->iteration++) {\n"; |
|
88 $output .= "\$_smarty_tpl->tpl_vars[$var]->first = \$_smarty_tpl->tpl_vars[$var]->iteration == 1;"; |
|
89 $output .= "\$_smarty_tpl->tpl_vars[$var]->last = \$_smarty_tpl->tpl_vars[$var]->iteration == \$_smarty_tpl->tpl_vars[$var]->total;"; |
|
90 } |
|
91 $output .= "?>"; |
|
92 |
|
93 $this->openTag($compiler, 'for', array('for', $compiler->nocache)); |
|
94 // maybe nocache because of nocache variables |
|
95 $compiler->nocache = $compiler->nocache | $compiler->tag_nocache; |
|
96 // return compiled code |
|
97 return $output; |
|
98 } |
|
99 } |
|
100 |
|
101 /** |
|
102 * Smarty Internal Plugin Compile Forelse Class |
|
103 * |
|
104 * @package Smarty |
|
105 * @subpackage Compiler |
|
106 */ |
|
107 class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase { |
|
108 /** |
|
109 * Compiles code for the {forelse} tag |
|
110 * |
|
111 * @param array $args array with attributes from parser |
|
112 * @param object $compiler compiler object |
|
113 * @param array $parameter array with compilation parameter |
|
114 * |
|
115 * @return string compiled code |
|
116 */ |
|
117 public function compile($args, $compiler, $parameter) { |
|
118 // check and get attributes |
|
119 $_attr = $this->getAttributes($compiler, $args); |
|
120 |
|
121 list($openTag, $nocache) = $this->closeTag($compiler, array('for')); |
|
122 $this->openTag($compiler, 'forelse', array('forelse', $nocache)); |
|
123 |
|
124 return "<?php }} else { ?>"; |
|
125 } |
|
126 } |
|
127 |
|
128 /** |
|
129 * Smarty Internal Plugin Compile Forclose Class |
|
130 * |
|
131 * @package Smarty |
|
132 * @subpackage Compiler |
|
133 */ |
|
134 class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase { |
|
135 /** |
|
136 * Compiles code for the {/for} tag |
|
137 * |
|
138 * @param array $args array with attributes from parser |
|
139 * @param object $compiler compiler object |
|
140 * @param array $parameter array with compilation parameter |
|
141 * |
|
142 * @return string compiled code |
|
143 */ |
|
144 public function compile($args, $compiler, $parameter) { |
|
145 // check and get attributes |
|
146 $_attr = $this->getAttributes($compiler, $args); |
|
147 // must endblock be nocache? |
|
148 if ($compiler->nocache) { |
|
149 $compiler->tag_nocache = true; |
|
150 } |
|
151 |
|
152 list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('for', 'forelse')); |
|
153 |
|
154 if ($openTag == 'forelse') { |
|
155 return "<?php } ?>"; |
|
156 } else { |
|
157 return "<?php }} ?>"; |
|
158 } |
|
159 } |
|
160 } |