49 public function compile($args, $compiler, $parameter)
51 $tpl = $compiler->template;
55 $from = $_attr[
'from'];
56 $item = $_attr[
'item'];
57 if (!strncmp(
"\$_smarty_tpl->tpl_vars[$item]", $from, strlen($item) + 24)) {
58 $compiler->trigger_template_error(
"item variable {$item} may not be the same variable as at 'from'", $compiler->lex->taglineno);
61 if (isset($_attr[
'key'])) {
67 $this->
openTag($compiler,
'foreach', array(
'foreach', $compiler->nocache, $item, $key));
69 $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
71 if (isset($_attr[
'name'])) {
72 $name = $_attr[
'name'];
74 $SmartyVarName =
'$smarty.foreach.' . trim($name,
'\'"') . '.';
79 $ItemVarName = '$' . trim($item, '\'"') . '@
';
80 // evaluates which Smarty variables and properties have to be computed
82 $usesSmartyFirst = strpos($tpl->source->content, $SmartyVarName . 'first
') !== false;
83 $usesSmartyLast = strpos($tpl->source->content, $SmartyVarName . 'last
') !== false;
84 $usesSmartyIndex = strpos($tpl->source->content, $SmartyVarName . 'index
') !== false;
85 $usesSmartyIteration = strpos($tpl->source->content, $SmartyVarName . 'iteration
') !== false;
86 $usesSmartyShow = strpos($tpl->source->content, $SmartyVarName . 'show
') !== false;
87 $usesSmartyTotal = strpos($tpl->source->content, $SmartyVarName . 'total
') !== false;
89 $usesSmartyFirst = false;
90 $usesSmartyLast = false;
91 $usesSmartyTotal = false;
92 $usesSmartyShow = false;
95 $usesPropFirst = $usesSmartyFirst || strpos($tpl->source->content, $ItemVarName . 'first
') !== false;
96 $usesPropLast = $usesSmartyLast || strpos($tpl->source->content, $ItemVarName . 'last
') !== false;
97 $usesPropIndex = $usesPropFirst || strpos($tpl->source->content, $ItemVarName . 'index
') !== false;
98 $usesPropIteration = $usesPropLast || strpos($tpl->source->content, $ItemVarName . 'iteration
') !== false;
99 $usesPropShow = strpos($tpl->source->content, $ItemVarName . 'show
') !== false;
100 $usesPropTotal = $usesSmartyTotal || $usesSmartyShow || $usesPropShow || $usesPropLast || strpos($tpl->source->content, $ItemVarName . 'total
') !== false;
101 // generate output code
103 $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable; \$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
105 $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
107 $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array
');}\n";
108 if ($usesPropTotal) {
109 $output .= " \$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
111 if ($usesPropIteration) {
112 $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
114 if ($usesPropIndex) {
115 $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
118 $output .= " \$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
121 if ($usesSmartyTotal) {
122 $output .= " \$_smarty_tpl->tpl_vars['smarty
']->value['foreach'][$name]['total
'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
124 if ($usesSmartyIteration) {
125 $output .= " \$_smarty_tpl->tpl_vars['smarty
']->value['foreach'][$name]['iteration
']=0;\n";
127 if ($usesSmartyIndex) {
128 $output .= " \$_smarty_tpl->tpl_vars['smarty
']->value['foreach'][$name]['index
']=-1;\n";
130 if ($usesSmartyShow) {
131 $output .= " \$_smarty_tpl->tpl_vars['smarty
']->value['foreach'][$name]['show
']=(\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
134 $output .= "foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
136 $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
138 if ($usesPropIteration) {
139 $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
141 if ($usesPropIndex) {
142 $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
144 if ($usesPropFirst) {
145 $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
148 $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
151 if ($usesSmartyFirst) {
152 $output .= " \$_smarty_tpl->tpl_vars['smarty
']->value['foreach'][$name]['first
'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
154 if ($usesSmartyIteration) {
155 $output .= " \$_smarty_tpl->tpl_vars['smarty
']->value['foreach'][$name]['iteration
']++;\n";
157 if ($usesSmartyIndex) {
158 $output .= " \$_smarty_tpl->tpl_vars['smarty
']->value['foreach'][$name]['index
']++;\n";
160 if ($usesSmartyLast) {
161 $output .= " \$_smarty_tpl->tpl_vars['smarty
']->value['foreach'][$name]['last
'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
176 class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase {
186 public function compile($args, $compiler, $parameter)
188 // check and get attributes
189 $_attr = $this->getAttributes($compiler, $args);
191 list($openTag, $nocache, $item, $key) = $this->closeTag($compiler, array('foreach'));
192 $this->openTag($compiler, 'foreachelse
', array('foreachelse
', $nocache, $item, $key));
194 return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
205 class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase {
215 public function compile($args, $compiler, $parameter)
217 // check and get attributes
218 $_attr = $this->getAttributes($compiler, $args);
219 // must endblock be nocache?
220 if ($compiler->nocache) {
221 $compiler->tag_nocache = true;
224 list($openTag, $compiler->nocache, $item, $key) = $this->closeTag($compiler, array('foreach', 'foreachelse
'));
compile($args, $compiler, $parameter)
Compiles code for the {foreach} tag.
openTag($compiler, $openTag, $data=null)
Push opening tag name on stack.
if(!empty($_GET['id'])) if(isset($_POST['form_name'], $_POST['form_subject'], $_POST['form_text'], $_POST['form_lang'])&&empty($_GET['id'])) if(empty($_GET['id'])) $tpl
getAttributes($compiler, $attributes)
This function checks if the attributes passed are valid.