aboutsummaryrefslogtreecommitdiffstats
path: root/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php')
-rw-r--r--library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php151
1 files changed, 105 insertions, 46 deletions
diff --git a/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php b/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php
index fc77f5749..82ea49637 100644
--- a/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php
+++ b/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php
@@ -64,103 +64,152 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
$key = null;
}
- $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key));
+ $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key, true));
// maybe nocache because of nocache variables
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
if (isset($_attr['name'])) {
- $name = $_attr['name'];
+ $name = trim($_attr['name'], '\'"');
$has_name = true;
- $SmartyVarName = '$smarty.foreach.' . trim($name, '\'"') . '.';
+ $SmartyVarName = "\$smarty.foreach.{$name}.";
} else {
- $name = null;
$has_name = false;
}
$ItemVarName = '$' . trim($item, '\'"') . '@';
// evaluates which Smarty variables and properties have to be computed
+
if ($has_name) {
- $usesSmartyFirst = strpos($compiler->lex->data, $SmartyVarName . 'first') !== false;
- $usesSmartyLast = strpos($compiler->lex->data, $SmartyVarName . 'last') !== false;
- $usesSmartyIndex = strpos($compiler->lex->data, $SmartyVarName . 'index') !== false;
- $usesSmartyIteration = strpos($compiler->lex->data, $SmartyVarName . 'iteration') !== false;
- $usesSmartyShow = strpos($compiler->lex->data, $SmartyVarName . 'show') !== false;
- $usesSmartyTotal = strpos($compiler->lex->data, $SmartyVarName . 'total') !== false;
+ $useSmartyForeach = $usesSmartyFirst = strpos($compiler->lex->data, $SmartyVarName . 'first') !== false;
+ $useSmartyForeach = ($usesSmartyLast = strpos($compiler->lex->data, $SmartyVarName . 'last') !== false) || $useSmartyForeach;
+ $useSmartyForeach = ($usesSmartyIndex = strpos($compiler->lex->data, $SmartyVarName . 'index') !== false) || $useSmartyForeach;
+ $useSmartyForeach = ($usesSmartyIteration = (!$usesSmartyIndex && ($usesSmartyFirst || $usesSmartyLast)) || strpos($compiler->lex->data, $SmartyVarName . 'iteration') !== false) || $useSmartyForeach;
+ $useSmartyForeach = ($usesSmartyShow = strpos($compiler->lex->data, $SmartyVarName . 'show') !== false) || $useSmartyForeach;
+ $useSmartyForeach = ($usesSmartyTotal = $usesSmartyLast ||strpos($compiler->lex->data, $SmartyVarName . 'total') !== false) || $useSmartyForeach;
} else {
$usesSmartyFirst = false;
$usesSmartyLast = false;
$usesSmartyTotal = false;
$usesSmartyShow = false;
+ $useSmartyForeach = false;
}
- $usesPropFirst = $usesSmartyFirst || strpos($compiler->lex->data, $ItemVarName . 'first') !== false;
- $usesPropLast = $usesSmartyLast || strpos($compiler->lex->data, $ItemVarName . 'last') !== false;
- $usesPropIndex = $usesPropFirst || strpos($compiler->lex->data, $ItemVarName . 'index') !== false;
- $usesPropIteration = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'iteration') !== false;
+ $usesPropKey = strpos($compiler->lex->data, $ItemVarName . 'key') !== false;
+ $usesPropFirst = strpos($compiler->lex->data, $ItemVarName . 'first') !== false;
+ $usesPropLast = strpos($compiler->lex->data, $ItemVarName . 'last') !== false;
+ $usesPropIndex = strpos($compiler->lex->data, $ItemVarName . 'index') !== false;
+ $usesPropIteration = (!$usesPropIndex && ($usesPropFirst || $usesPropLast)) || strpos($compiler->lex->data, $ItemVarName . 'iteration') !== false;
$usesPropShow = strpos($compiler->lex->data, $ItemVarName . 'show') !== false;
- $usesPropTotal = $usesSmartyTotal || $usesSmartyShow || $usesPropShow || $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false;
+ $usesPropTotal = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false;
+
+ $keyTerm = '';
+ if ($usesPropKey) {
+ $keyTerm = "\$_smarty_tpl->tpl_vars[$item]->key => ";
+ } elseif ($key != null) {
+ $keyTerm = "\$_smarty_tpl->tpl_vars[$key]->value => ";
+ }
// generate output code
- $output = "<?php ";
- $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable; \$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
+ $output = "<?php\n";
+ $output .= "\$_from = $from;\n";
+ $output .= "if (!is_array(\$_from) && !is_object(\$_from)) {\n";
+ $output .= "settype(\$_from, 'array');\n";
+ $output .= "}\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
if ($key != null) {
- $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
}
- $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
if ($usesPropTotal) {
- $output .= " \$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
}
if ($usesPropIteration) {
- $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
}
if ($usesPropIndex) {
- $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
}
if ($usesPropShow) {
- $output .= " \$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
+ if ($usesPropTotal) {
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
+ } else {
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->_count(\$_from) > 0);\n";
+ }
}
if ($has_name) {
+ $prop = array();
if ($usesSmartyTotal) {
- $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
+ $prop['total'] = "'total' => ";
+ $prop['total'] .= $usesSmartyShow ? '$total = ' : '';
+ $prop['total'] .= '$_smarty_tpl->_count($_from)';
}
if ($usesSmartyIteration) {
- $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
+ $prop['iteration'] = "'iteration' => 0";
}
if ($usesSmartyIndex) {
- $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
+ $prop['index'] = "'index' => -1";
}
if ($usesSmartyShow) {
- $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['show']=(\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
+ $prop['show'] = "'show' => ";
+ if ($usesSmartyTotal) {
+ $prop['show'] .= "(\$total > 0)";
+ } else {
+ $prop['show'] .= "(\$_smarty_tpl->_count(\$_from) > 0)";
+ }
+ }
+ if ($useSmartyForeach) {
+ $_vars = 'array(' . join(', ', $prop) . ')';
+ $foreachVar = "'__foreach_{$name}'";
+ $output .= "\$_smarty_tpl->tpl_vars[$foreachVar] = new Smarty_Variable({$_vars});\n";
}
}
- $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";
- if ($key != null) {
- $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
+ $output .= "foreach (\$_from as {$keyTerm}\$_smarty_tpl->tpl_vars[$item]->value) {\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
+ if ($key != null && $usesPropKey) {
+ $output .= "\$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
}
if ($usesPropIteration) {
- $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
}
if ($usesPropIndex) {
- $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->index++;\n";
}
if ($usesPropFirst) {
- $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
+ if ($usesPropIndex) {
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index == 0;\n";
+ } else {
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->iteration == 1;\n";
+ }
}
if ($usesPropLast) {
- $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
+ if ($usesPropIndex) {
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->index + 1 == \$_smarty_tpl->tpl_vars[$item]->total;\n";
+ } else {
+ $output .= "\$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration == \$_smarty_tpl->tpl_vars[$item]->total;\n";
+ }
}
if ($has_name) {
- if ($usesSmartyFirst) {
- $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
- }
if ($usesSmartyIteration) {
- $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration']++;\n";
}
if ($usesSmartyIndex) {
- $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['index']++;\n";
+ }
+ if ($usesSmartyFirst) {
+ if ($usesSmartyIndex) {
+ $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] == 0;\n";
+ } else {
+ $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == 1;\n";
+ }
}
if ($usesSmartyLast) {
- $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
+ if ($usesSmartyIndex) {
+ $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] + 1 == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
+ } else {
+ $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
+ }
}
}
+ $itemName = trim($item,"'\"");
+ $output .= "\$foreach_{$itemName}_Sav = \$_smarty_tpl->tpl_vars[$item];\n";
$output .= "?>";
return $output;
@@ -189,10 +238,14 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
- list($openTag, $nocache, $item, $key) = $this->closeTag($compiler, array('foreach'));
- $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key));
-
- return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
+ list($openTag, $nocache, $item, $key, $foo) = $this->closeTag($compiler, array('foreach'));
+ $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key, false));
+ $itemName = trim($item,"'\"");
+ $output = "<?php\n";
+ $output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreach_{$itemName}_Sav;\n";
+ $output .= "}\n";
+ $output .= "if (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
+ return $output;
}
}
@@ -222,8 +275,14 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
$compiler->tag_nocache = true;
}
- list($openTag, $compiler->nocache, $item, $key) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
+ list($openTag, $compiler->nocache, $item, $key, $restore) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
+ $itemName = trim($item,"'\"");
+ $output = "<?php\n";
+ if ($restore) {
+ $output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreach_{$itemName}_Sav;\n";
+ }
+ $output .= "}\n?>";
- return "<?php } ?>";
+ return $output;
}
}