summaryrefslogtreecommitdiff
path: root/lib/smarty3/sysplugins/smarty_internal_compile_foreach.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/smarty3/sysplugins/smarty_internal_compile_foreach.php')
-rw-r--r--lib/smarty3/sysplugins/smarty_internal_compile_foreach.php89
1 files changed, 52 insertions, 37 deletions
diff --git a/lib/smarty3/sysplugins/smarty_internal_compile_foreach.php b/lib/smarty3/sysplugins/smarty_internal_compile_foreach.php
index 56d760c..78b45ea 100644
--- a/lib/smarty3/sysplugins/smarty_internal_compile_foreach.php
+++ b/lib/smarty3/sysplugins/smarty_internal_compile_foreach.php
@@ -78,16 +78,16 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
/**
* Compiles code for the {foreach} tag
*
- * @param array $args array with attributes from parser
- * @param \Smarty_Internal_TemplateCompilerBase $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
*
* @return string compiled code
* @throws \SmartyCompilerException
+ * @throws \SmartyException
*/
- public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
+ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
- $compiler->loopNesting ++;
+ $compiler->loopNesting++;
// init
$this->isNamed = false;
// check and get attributes
@@ -118,15 +118,17 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
$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);
+ if (isset($attributes[ $a ]) && $attributes[ $a ] === $fromName) {
+ $compiler->trigger_template_error(
+ "'{$a}' and 'from' may not have same variable name '{$fromName}'",
+ null,
+ true
+ );
}
}
}
-
$itemVar = "\$_smarty_tpl->tpl_vars['{$item}']";
- $local = '$__foreach_' . $attributes[ 'item' ] . '_' . $this->counter ++ . '_';
+ $local = '$__foreach_' . $attributes[ 'item' ] . '_' . $this->counter++ . '_';
// search for used tag attributes
$itemAttr = array();
$namedAttr = array();
@@ -137,7 +139,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
if (!empty($this->matchResults[ 'named' ])) {
$namedAttr = $this->matchResults[ 'named' ];
}
- if (isset($_attr[ 'properties' ]) && preg_match_all("/['](.*?)[']/", $_attr[ 'properties' ], $match)) {
+ if (isset($_attr[ 'properties' ]) && preg_match_all('/[\'](.*?)[\']/', $_attr[ 'properties' ], $match)) {
foreach ($match[ 1 ] as $prop) {
if (in_array($prop, $this->itemProperties)) {
$itemAttr[ $prop ] = true;
@@ -187,8 +189,11 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
}
$needTotal = isset($itemAttr[ 'total' ]);
// Register tag
- $this->openTag($compiler, 'foreach',
- array('foreach', $compiler->nocache, $local, $itemVar, empty($itemAttr) ? 1 : 2));
+ $this->openTag(
+ $compiler,
+ 'foreach',
+ array('foreach', $compiler->nocache, $local, $itemVar, empty($itemAttr) ? 1 : 2)
+ );
// maybe nocache because of nocache variables
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
// generate output code
@@ -229,9 +234,9 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
$output .= "{$itemVar}->first = !{$itemVar}->index;\n";
}
if (isset($itemAttr[ 'last' ])) {
- $output .= "{$itemVar}->last = {$itemVar}->iteration == {$itemVar}->total;\n";
+ $output .= "{$itemVar}->last = {$itemVar}->iteration === {$itemVar}->total;\n";
}
- if ($this->isNamed) {
+ if (isset($foreachVar)) {
if (isset($namedAttr[ 'iteration' ])) {
$output .= "{$foreachVar}->value['iteration']++;\n";
}
@@ -242,16 +247,27 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
$output .= "{$foreachVar}->value['first'] = !{$foreachVar}->value['index'];\n";
}
if (isset($namedAttr[ 'last' ])) {
- $output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] == {$foreachVar}->value['total'];\n";
+ $output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] === {$foreachVar}->value['total'];\n";
}
}
if (!empty($itemAttr)) {
$output .= "{$local}saved = {$itemVar};\n";
}
- $output .= "?>";
-
+ $output .= '?>';
return $output;
}
+
+ /**
+ * Compiles code for to restore saved template variables
+ *
+ * @param int $levels number of levels to restore
+ *
+ * @return string compiled code
+ */
+ public function compileRestore($levels)
+ {
+ return "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl, {$levels});";
+ }
}
/**
@@ -265,24 +281,22 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
/**
* Compiles code for the {foreachelse} tag
*
- * @param array $args array with attributes from parser
- * @param \Smarty_Internal_TemplateCompilerBase $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
*
* @return string compiled code
*/
- public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
+ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
-
list($openTag, $nocache, $local, $itemVar, $restore) = $this->closeTag($compiler, array('foreach'));
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, 0));
$output = "<?php\n";
- if ($restore == 2) {
+ if ($restore === 2) {
$output .= "{$itemVar} = {$local}saved;\n";
}
- $output .= "}\n} else {\n?>\n";
+ $output .= "}\n} else {\n?>";
return $output;
}
}
@@ -298,33 +312,34 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
/**
* Compiles code for the {/foreach} tag
*
- * @param array $args array with attributes from parser
- * @param \Smarty_Internal_TemplateCompilerBase $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
*
* @return string compiled code
+ * @throws \SmartyCompilerException
*/
- public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
+ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
- $compiler->loopNesting --;
+ $compiler->loopNesting--;
// must endblock be nocache?
if ($compiler->nocache) {
$compiler->tag_nocache = true;
}
-
- list($openTag, $compiler->nocache, $local, $itemVar, $restore) =
- $this->closeTag($compiler, array('foreach', 'foreachelse'));
+ list(
+ $openTag, $compiler->nocache, $local, $itemVar, $restore
+ ) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
$output = "<?php\n";
-
- if ($restore == 2) {
+ if ($restore === 2) {
$output .= "{$itemVar} = {$local}saved;\n";
}
if ($restore > 0) {
$output .= "}\n";
}
$output .= "}\n";
- $output .= "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl);\n";
- $output .= "?>\n";
+ /* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
+ $foreachCompiler = $compiler->getTagCompiler('foreach');
+ $output .= $foreachCompiler->compileRestore(1);
+ $output .= "?>";
return $output;
}
}