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.php345
1 files changed, 201 insertions, 144 deletions
diff --git a/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php b/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php
index 82ea49637..876ed026a 100644
--- a/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php
+++ b/library/Smarty/libs/sysplugins/smarty_internal_compile_foreach.php
@@ -14,7 +14,7 @@
* @package Smarty
* @subpackage Compiler
*/
-class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
+class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_ForeachSection
{
/**
* Attribute definition: Overwrites base class.
@@ -23,6 +23,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
* @see Smarty_Internal_CompileBase
*/
public $required_attributes = array('from', 'item');
+
/**
* Attribute definition: Overwrites base class.
*
@@ -30,6 +31,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
* @see Smarty_Internal_CompileBase
*/
public $optional_attributes = array('name', 'key');
+
/**
* Attribute definition: Overwrites base class.
*
@@ -39,177 +41,228 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
public $shorttag_order = array('from', 'item', 'key', 'name');
/**
+ * counter
+ *
+ * @var int
+ */
+ public $counter = 0;
+
+ /**
+ * Name of this tag
+ *
+ * @var string
+ */
+ public $tagName = 'foreach';
+
+ /**
+ * Valid properties of $smarty.foreach.name.xxx variable
+ *
+ * @var array
+ */
+ public static $nameProperties = array('first', 'last', 'index', 'iteration', 'show', 'total');
+
+ /**
+ * Valid properties of $item@xxx variable
+ *
+ * @var array
+ */
+ public $itemProperties = array('first', 'last', 'index', 'iteration', 'show', 'total', 'key');
+
+ /**
+ * Flag if tag had name attribute
+ *
+ * @var bool
+ */
+ public $isNamed = false;
+
+ /**
* Compiles code for the {foreach} tag
*
- * @param array $args array with attributes from parser
- * @param object $compiler compiler object
- * @param array $parameter array with compilation parameter
+ * @param array $args array with attributes from parser
+ * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
+ * @param array $parameter array with compilation parameter
*
* @return string compiled code
+ * @throws \SmartyCompilerException
*/
- public function compile($args, $compiler, $parameter)
+ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
+ $compiler->loopNesting ++;
+ // init
+ $this->isNamed = false;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
-
$from = $_attr['from'];
- $item = $_attr['item'];
- if (!strncmp("\$_smarty_tpl->tpl_vars[$item]", $from, strlen($item) + 24)) {
- $compiler->trigger_template_error("item variable {$item} may not be the same variable as at 'from'", $compiler->lex->taglineno);
+ $item = $compiler->getId($_attr['item']);
+ if ($item === false) {
+ $item = $compiler->getVariableName($_attr['item']);
}
-
+ $attributes = array('item' => $item);
if (isset($_attr['key'])) {
- $key = $_attr['key'];
- } else {
- $key = null;
+ $key = $compiler->getId($_attr['key']);
+ if ($key === false) {
+ $key = $compiler->getVariableName($_attr['key']);
+ }
+ $attributes['key'] = $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 = trim($_attr['name'], '\'"');
- $has_name = true;
- $SmartyVarName = "\$smarty.foreach.{$name}.";
- } else {
- $has_name = false;
- }
- $ItemVarName = '$' . trim($item, '\'"') . '@';
- // evaluates which Smarty variables and properties have to be computed
-
- if ($has_name) {
- $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;
+ $this->isNamed = true;
+ $attributes['name'] = $compiler->getId($_attr['name']);
+ }
+ foreach ($attributes as $a => $v) {
+ if ($v === false) {
+ $compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
+ }
+ }
+ $fromName = $compiler->getVariableName($_attr['from']);
+ if ($fromName) {
+ foreach (array('item', 'key') as $a) {
+ if (isset($attributes[$a]) && $attributes[$a] == $fromName) {
+ $compiler->trigger_template_error("'{$a}' and 'from' may not have same variable name '{$fromName}'",
+ null, true);
+ }
+ }
}
- $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 = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false;
+ $itemVar = "\$_smarty_tpl->tpl_vars['{$item}']";
+ $local = '$__foreach_' . (isset($attributes['name']) ? $attributes['name'] : $attributes['item']) . '_' .
+ $this->counter ++ . '_';
+ $needIteration = false;
+ // search for used tag attributes
+ $itemAttr = array();
+ $namedAttr = array();
+ $this->scanForProperties($attributes, $compiler);
+ if (!empty($this->matchResults['item'])) {
+ $itemAttr = $this->matchResults['item'];
+ }
+ if (!empty($this->matchResults['named'])) {
+ $namedAttr = $this->matchResults['named'];
+ }
+ if (isset($itemAttr['last'])) {
+ $needIteration = true;
+ }
+ if (isset($namedAttr['last'])) {
+ $needIteration = true;
+ }
$keyTerm = '';
- if ($usesPropKey) {
- $keyTerm = "\$_smarty_tpl->tpl_vars[$item]->key => ";
- } elseif ($key != null) {
- $keyTerm = "\$_smarty_tpl->tpl_vars[$key]->value => ";
+ if (isset($itemAttr['key'])) {
+ $keyTerm = "{$itemVar}->key => ";
+ } elseif (isset($attributes['key'])) {
+ $keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => ";
}
+
+ $saveVars = array();
+ $restoreVars = array();
+ if ($this->isNamed) {
+ $foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
+ if (!empty($namedAttr)) {
+ $saveVars['saved'] = "isset({$foreachVar}) ? {$foreachVar} : false;";
+ $restoreVars[] = "if ({$local}saved) {\n{$foreachVar} = {$local}saved;\n}\n";
+ }
+ }
+ foreach (array('item', 'key') as $a) {
+ if (isset($attributes[$a])) {
+ $saveVars['saved_' . $a] =
+ "isset(\$_smarty_tpl->tpl_vars['{$attributes[$a]}']) ? \$_smarty_tpl->tpl_vars['{$attributes[$a]}'] : false;";
+ $restoreVars[] =
+ "if ({$local}saved_{$a}) {\n\$_smarty_tpl->tpl_vars['{$attributes[$a]}'] = {$local}saved_{$a};\n}\n";
+ }
+ }
+ $this->openTag($compiler, 'foreach',
+ array('foreach', $compiler->nocache, $local, $restoreVars, $itemVar, true));
+ // maybe nocache because of nocache variables
+ $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
+
// generate output code
$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";
+ foreach ($saveVars as $k => $code) {
+ $output .= "{$local}{$k} = {$code}\n";
}
- if ($usesPropTotal) {
- $output .= "\$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
+ $output .= "{$itemVar} = new Smarty_Variable();\n";
+ $output .= "{$local}total = \$_smarty_tpl->smarty->ext->_foreach->count(\$_from);\n";
+ if (isset($itemAttr['show'])) {
+ $output .= "{$itemVar}->show = ({$local}total > 0);\n";
}
- if ($usesPropIteration) {
- $output .= "\$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
+ if (isset($itemAttr['total'])) {
+ $output .= "{$itemVar}->total= {$local}total;\n";
}
- if ($usesPropIndex) {
- $output .= "\$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
- }
- if ($usesPropShow) {
- 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) {
+ if ($this->isNamed) {
$prop = array();
- if ($usesSmartyTotal) {
- $prop['total'] = "'total' => ";
- $prop['total'] .= $usesSmartyShow ? '$total = ' : '';
- $prop['total'] .= '$_smarty_tpl->_count($_from)';
+ if (isset($namedAttr['total'])) {
+ $prop['total'] = "'total' => {$local}total";
}
- if ($usesSmartyIteration) {
+ if (isset($namedAttr['iteration'])) {
$prop['iteration'] = "'iteration' => 0";
}
- if ($usesSmartyIndex) {
+ if (isset($namedAttr['index'])) {
$prop['index'] = "'index' => -1";
}
- if ($usesSmartyShow) {
- $prop['show'] = "'show' => ";
- if ($usesSmartyTotal) {
- $prop['show'] .= "(\$total > 0)";
- } else {
- $prop['show'] .= "(\$_smarty_tpl->_count(\$_from) > 0)";
- }
+ if (isset($namedAttr['show'])) {
+ $prop['show'] = "'show' => ({$local}total > 0)";
}
- if ($useSmartyForeach) {
+ if (!empty($namedAttr)) {
$_vars = 'array(' . join(', ', $prop) . ')';
- $foreachVar = "'__foreach_{$name}'";
- $output .= "\$_smarty_tpl->tpl_vars[$foreachVar] = new Smarty_Variable({$_vars});\n";
+ $output .= "{$foreachVar} = new Smarty_Variable({$_vars});\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";
+ $output .= "if ({$local}total) {\n";
+ if (isset($attributes['key'])) {
+ $output .= "\$_smarty_tpl->tpl_vars['{$key}'] = new Smarty_Variable();\n";
}
- if ($usesPropIteration) {
- $output .= "\$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
+ if (isset($namedAttr['first']) || isset($itemAttr['first'])) {
+ $output .= "{$local}first = true;\n";
}
- if ($usesPropIndex) {
- $output .= "\$_smarty_tpl->tpl_vars[$item]->index++;\n";
+ if (isset($itemAttr['iteration'])) {
+ $output .= "{$itemVar}->iteration=0;\n";
}
- if ($usesPropFirst) {
- 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 (isset($itemAttr['index'])) {
+ $output .= "{$itemVar}->index=-1;\n";
}
- if ($usesPropLast) {
- 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 ($needIteration) {
+ $output .= "{$local}iteration=0;\n";
+ }
+ $output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n";
+ if (isset($attributes['key']) && isset($itemAttr['key'])) {
+ $output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n";
+ }
+ if (isset($itemAttr['iteration'])) {
+ $output .= "{$itemVar}->iteration++;\n";
+ }
+ if (isset($itemAttr['index'])) {
+ $output .= "{$itemVar}->index++;\n";
}
- if ($has_name) {
- if ($usesSmartyIteration) {
- $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration']++;\n";
+ if ($needIteration) {
+ $output .= "{$local}iteration++;\n";
+ }
+ if (isset($itemAttr['first'])) {
+ $output .= "{$itemVar}->first = {$local}first;\n";
+ }
+ if (isset($itemAttr['last'])) {
+ $output .= "{$itemVar}->last = {$local}iteration == {$local}total;\n";
+ }
+ if ($this->isNamed) {
+ if (isset($namedAttr['iteration'])) {
+ $output .= "{$foreachVar}->value['iteration']++;\n";
}
- if ($usesSmartyIndex) {
- $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['index']++;\n";
+ if (isset($namedAttr['index'])) {
+ $output .= "{$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 (isset($namedAttr['first'])) {
+ $output .= "{$foreachVar}->value['first'] = {$local}first;\n";
}
- if ($usesSmartyLast) {
- 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";
- }
+ if (isset($namedAttr['last'])) {
+ $output .= "{$foreachVar}->value['last'] = {$local}iteration == {$local}total;\n";
}
}
- $itemName = trim($item,"'\"");
- $output .= "\$foreach_{$itemName}_Sav = \$_smarty_tpl->tpl_vars[$item];\n";
+ if (isset($namedAttr['first']) || isset($itemAttr['first'])) {
+ $output .= "{$local}first = false;\n";
+ }
+ $output .= "{$local}saved_local_item = {$itemVar};\n";
$output .= "?>";
return $output;
@@ -227,24 +280,23 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
/**
* Compiles code for the {foreachelse} tag
*
- * @param array $args array with attributes from parser
- * @param object $compiler compiler object
- * @param array $parameter array with compilation parameter
+ * @param array $args array with attributes from parser
+ * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
+ * @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
- public function compile($args, $compiler, $parameter)
+ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
- list($openTag, $nocache, $item, $key, $foo) = $this->closeTag($compiler, array('foreach'));
- $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key, false));
- $itemName = trim($item,"'\"");
+ list($openTag, $nocache, $local, $restoreVars, $itemVar, $foo) = $this->closeTag($compiler, array('foreach'));
+ $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $restoreVars, $itemVar, false));
$output = "<?php\n";
- $output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreach_{$itemName}_Sav;\n";
+ $output .= "{$itemVar} = {$local}saved_local_item;\n";
$output .= "}\n";
- $output .= "if (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
+ $output .= "} else {\n?>";
return $output;
}
}
@@ -260,28 +312,33 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
/**
* Compiles code for the {/foreach} tag
*
- * @param array $args array with attributes from parser
- * @param object $compiler compiler object
- * @param array $parameter array with compilation parameter
+ * @param array $args array with attributes from parser
+ * @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
+ * @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
- public function compile($args, $compiler, $parameter)
+ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
- // check and get attributes
- $_attr = $this->getAttributes($compiler, $args);
+ $compiler->loopNesting --;
// must endblock be nocache?
if ($compiler->nocache) {
$compiler->tag_nocache = true;
}
- list($openTag, $compiler->nocache, $item, $key, $restore) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
- $itemName = trim($item,"'\"");
+ list($openTag, $compiler->nocache, $local, $restoreVars, $itemVar, $restore) =
+ $this->closeTag($compiler, array('foreach', 'foreachelse'));
$output = "<?php\n";
+
if ($restore) {
- $output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreach_{$itemName}_Sav;\n";
+ $output .= "{$itemVar} = {$local}saved_local_item;\n";
+ $output .= "}\n";
+ }
+ $output .= "}\n";
+ foreach ($restoreVars as $restore) {
+ $output .= $restore;
}
- $output .= "}\n?>";
+ $output .= "?>";
return $output;
}