3 * Smarty Internal Plugin Compile For
5 * Compiles the {for} {forelse} {/for} tags
13 * Smarty Internal Plugin Compile For Class
15 class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase {
17 * Compiles code for the {for} tag
19 * Smarty 3 does implement two different sytaxes:
21 * - {for $var in $array}
22 * For looping over arrays or iterators
24 * - {for $x=0; $x<$y; $x++}
27 * The parser is gereration different sets of attribute by which this compiler can
28 * determin which syntax is used.
30 * @param array $args array with attributes from parser
31 * @param object $compiler compiler object
32 * @param array $parameter array with compilation parameter
33 * @return string compiled code
35 public function compile($args, $compiler, $parameter)
37 $this->compiler = $compiler;
38 if ($parameter == 0) {
39 $this->required_attributes = array('start','to');
40 $this->optional_attributes = array('max','step');
42 $this->required_attributes = array('start','ifexp','var','step');
43 $this->optional_attributes = array();
45 // check and get attributes
46 $_attr = $this->_get_attributes($args);
48 $local_vars = array();
51 if ($parameter == 1) {
52 foreach ($_attr['start'] as $_statement) {
53 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
54 $output .= " \$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value];\n";
55 $compiler->local_var[$_statement['var']] = true;
56 $local_vars[] = $_statement['var'];
58 $output .= " if ($_attr[ifexp]){ for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[var]]->value$_attr[step]){\n";
60 $_statement = $_attr['start'];
61 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
62 $compiler->local_var[$_statement['var']] = true;
63 $local_vars[] = $_statement['var'];
64 if (isset($_attr['step'])) {
65 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = $_attr[step];";
67 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->step = 1;";
69 if (isset($_attr['max'])) {
70 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)min(ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step)),$_attr[max]);\n";
72 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->total = (int)ceil((\$_smarty_tpl->tpl_vars[$_statement[var]]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$_statement[var]]->step));\n";
74 $output .= "if (\$_smarty_tpl->tpl_vars[$_statement[var]]->total > 0){\n";
75 $output .= "for (\$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value], \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration = 1;\$_smarty_tpl->tpl_vars[$_statement[var]]->iteration <= \$_smarty_tpl->tpl_vars[$_statement[var]]->total;\$_smarty_tpl->tpl_vars[$_statement[var]]->value += \$_smarty_tpl->tpl_vars[$_statement[var]]->step, \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration++){\n";
76 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->first = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == 1;";
77 $output .= "\$_smarty_tpl->tpl_vars[$_statement[var]]->last = \$_smarty_tpl->tpl_vars[$_statement[var]]->iteration == \$_smarty_tpl->tpl_vars[$_statement[var]]->total;";
81 $this->_open_tag('for', array('for', $this->compiler->nocache, $local_vars));
82 // maybe nocache because of nocache variables
83 $this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
84 // return compiled code
90 * Smarty Internal Plugin Compile Forelse Class
92 class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase {
94 * Compiles code for the {forelse} tag
96 * @param array $args array with attributes from parser
97 * @param object $compiler compiler object
98 * @param array $parameter array with compilation parameter
99 * @return string compiled code
101 public function compile($args, $compiler, $parameter)
103 $this->compiler = $compiler;
104 // check and get attributes
105 $_attr = $this->_get_attributes($args);
107 list($_open_tag, $nocache, $local_vars) = $this->_close_tag(array('for'));
108 $this->_open_tag('forelse', array('forelse', $nocache, $local_vars));
109 return "<?php }} else { ?>";
114 * Smarty Internal Plugin Compile Forclose Class
116 class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase {
118 * Compiles code for the {/for} tag
120 * @param array $args array with attributes from parser
121 * @param object $compiler compiler object
122 * @param array $parameter array with compilation parameter
123 * @return string compiled code
125 public function compile($args, $compiler, $parameter)
127 $this->compiler = $compiler;
128 // check and get attributes
129 $_attr = $this->_get_attributes($args);
130 // must endblock be nocache?
131 if ($this->compiler->nocache) {
132 $this->compiler->tag_nocache = true;
135 list($_open_tag, $this->compiler->nocache, $local_vars) = $this->_close_tag(array('for', 'forelse'));
137 foreach ($local_vars as $var) {
138 unset($compiler->local_var[$var]);
140 if ($_open_tag == 'forelse')
143 return "<?php }} ?>";