Skip to content

Commit

Permalink
- bugfix {foreach} did fail if from atrribute is a Generator class #128
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed Dec 16, 2015
1 parent e1eefdc commit fa3aed3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions libs/sysplugins/smarty_internal_compile_foreach.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
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";
}
Expand All @@ -210,7 +212,6 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
$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";
}
Expand All @@ -226,7 +227,9 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
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";
}
Expand Down Expand Up @@ -296,7 +299,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
$output = "<?php\n";
$output .= "{$itemVar} = {$local}saved_local_item;\n";
$output .= "}\n";
$output .= "} else {\n?>";
$output .= "if (!{$itemVar}->_loop) {\n?>";
return $output;
}
}
Expand Down Expand Up @@ -332,7 +335,6 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $

if ($restore) {
$output .= "{$itemVar} = {$local}saved_local_item;\n";
$output .= "}\n";
}
$output .= "}\n";
foreach ($restoreVars as $restore) {
Expand Down
3 changes: 3 additions & 0 deletions libs/sysplugins/smarty_internal_runtime_foreach.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function count($value)
// 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();
Expand Down

0 comments on commit fa3aed3

Please sign in to comment.