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