From d5ca4518bed5fc2aad6544b8df38b5d1fbf13d6d Mon Sep 17 00:00:00 2001 From: uwetews Date: Thu, 17 Sep 2015 21:01:48 +0200 Subject: [PATCH] - bugfix {foreach} first attribute was not correctly reset since commit 05a8fa2 of 02.08.2015 https://github.com/smarty-php/smarty/issues/90 --- change_log.txt | 3 ++ libs/Smarty.class.php | 2 +- .../smarty_internal_compile_foreach.php | 28 ++++++++++--------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/change_log.txt b/change_log.txt index 8c807dc8..5f2ed92f 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@  ===== 3.1.28-dev===== (xx.xx.2015) + 17.09.2015 + - bugfix {foreach} first attribute was not correctly reset since commit 05a8fa2 of 02.08.2015 https://github.com/smarty-php/smarty/issues/90 + 16.09.2015 - update compiler by moving no longer needed properties, code optimizations and other diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 7adfd78b..89e3b7b2 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -119,7 +119,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/61'; + const SMARTY_VERSION = '3.1.28-dev/62'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_foreach.php b/libs/sysplugins/smarty_internal_compile_foreach.php index f7f7b472..d1b6bdd5 100644 --- a/libs/sysplugins/smarty_internal_compile_foreach.php +++ b/libs/sysplugins/smarty_internal_compile_foreach.php @@ -87,7 +87,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo */ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) { - $compiler->loopNesting++; + $compiler->loopNesting ++; // init $this->isNamed = false; // check and get attributes @@ -118,7 +118,8 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo 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); + $compiler->trigger_template_error("'{$a}' and 'from' may not have same variable name '{$fromName}'", + null, true); } } } @@ -162,13 +163,14 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo } 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"; + $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)); + $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; @@ -256,9 +258,9 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo if (isset($namedAttr['last'])) { $output .= "{$foreachVar}->value['last'] = {$local}iteration == {$local}total;\n"; } - if (isset($namedAttr['first']) || isset($itemAttr['first'])) { - $output .= "{$local}first = false;\n"; - } + } + if (isset($namedAttr['first']) || isset($itemAttr['first'])) { + $output .= "{$local}first = false;\n"; } $output .= "{$local}saved_local_item = {$itemVar};\n"; $output .= "?>"; @@ -318,14 +320,14 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase */ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) { - $compiler->loopNesting--; + $compiler->loopNesting --; // must endblock be nocache? if ($compiler->nocache) { $compiler->tag_nocache = true; } - list($openTag, $compiler->nocache, $local, $restoreVars, $itemVar, $restore) = $this->closeTag($compiler, array('foreach', - 'foreachelse')); + list($openTag, $compiler->nocache, $local, $restoreVars, $itemVar, $restore) = + $this->closeTag($compiler, array('foreach', 'foreachelse')); $output = "