diff --git a/change_log.txt b/change_log.txt index dda0647f..cb7ec49f 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@  ===== 3.1.29-dev ===== (xx.xx.2015) + 16.12.2015 + - bugfix {foreach} did fail if from atrribute is a Generator class https://github.com/smarty-php/smarty/issues/128 + 15.12.2015 - bugfix {$smarty.cookies.foo} did return the $_COOKIE array not the 'foo' value https://github.com/smarty-php/smarty/issues/122 - bugfix a call to clearAllCache() and other should clear all internal template object caches (forum topic 25828) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index c0628524..628e22d4 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -118,7 +118,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.29-dev/5'; + const SMARTY_VERSION = '3.1.29-dev/6'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_foreach.php b/libs/sysplugins/smarty_internal_compile_foreach.php index 876ed026..fd301aab 100644 --- a/libs/sysplugins/smarty_internal_compile_foreach.php +++ b/libs/sysplugins/smarty_internal_compile_foreach.php @@ -183,8 +183,10 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo foreach ($saveVars as $k => $code) { $output .= "{$local}{$k} = {$code}\n"; } + if (isset($itemAttr['show']) || isset($itemAttr['total']) || isset($namedAttr['total']) || isset($namedAttr['show']) || isset($itemAttr['last']) || isset($namedAttr['last'])) { + $output .= "{$local}total = \$_smarty_tpl->smarty->ext->_foreach->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"; } @@ -210,7 +212,6 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo $output .= "{$foreachVar} = new Smarty_Variable({$_vars});\n"; } } - $output .= "if ({$local}total) {\n"; if (isset($attributes['key'])) { $output .= "\$_smarty_tpl->tpl_vars['{$key}'] = new Smarty_Variable();\n"; } @@ -226,7 +227,9 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo if ($needIteration) { $output .= "{$local}iteration=0;\n"; } + $output .= "{$itemVar}->_loop = false;\n"; $output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n"; + $output .= "{$itemVar}->_loop = true;\n"; if (isset($attributes['key']) && isset($itemAttr['key'])) { $output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n"; } @@ -296,7 +299,7 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase $output = ""; + $output .= "if (!{$itemVar}->_loop) {\n?>"; return $output; } } @@ -332,7 +335,6 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase if ($restore) { $output .= "{$itemVar} = {$local}saved_local_item;\n"; - $output .= "}\n"; } $output .= "}\n"; foreach ($restoreVars as $restore) { diff --git a/libs/sysplugins/smarty_internal_runtime_foreach.php b/libs/sysplugins/smarty_internal_runtime_foreach.php index 87bb0cf4..689636ad 100644 --- a/libs/sysplugins/smarty_internal_runtime_foreach.php +++ b/libs/sysplugins/smarty_internal_runtime_foreach.php @@ -27,6 +27,9 @@ class Smarty_Internal_Runtime_Foreach // thus rewind() and valid() methods may not be present return iterator_count($value->getIterator()); } elseif ($value instanceof Iterator) { + if ($value instanceof Generator) { + return 1; + } return iterator_count($value); } elseif ($value instanceof PDOStatement) { return $value->rowCount();