diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index dd187920..d85a874c 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -557,17 +557,16 @@ class Smarty_Compiler extends Smarty { $this->_syntax_error("missing 'item' attribute"); } - if (empty($attrs['name'])) { - $this->_syntax_error("missing 'name' attribute"); - } - $from = $attrs['from']; $item = $this->_dequote($attrs['item']); - $name = $attrs['name']; + if (isset($attrs['name'])) + $name = $attrs['name']; $output = '_foreach[$name])) unset(\$this->_foreach[$name]);\n"; - $foreach_props = "\$this->_foreach[$name]"; + if (isset($name)) { + $output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n"; + $foreach_props = "\$this->_foreach[$name]"; + } $key_part = ''; @@ -584,14 +583,19 @@ class Smarty_Compiler extends Smarty { } } - $output .= "{$foreach_props}['total'] = count((array)$from);\n"; - $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n"; - $output .= "if ({$foreach_props}['show']):\n"; - $output .= "{$foreach_props}['iteration'] = 0;\n"; - $output .= " foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n"; - $output .= " {$foreach_props}['iteration']++;\n"; - $output .= " {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n"; - $output .= " {$foreach_props}['last'] = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n"; + if (isset($name)) { + $output .= "{$foreach_props}['total'] = count((array)$from);\n"; + $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n"; + $output .= "if ({$foreach_props}['show']):\n"; + $output .= "{$foreach_props}['iteration'] = 0;\n"; + $output .= " foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n"; + $output .= " {$foreach_props}['iteration']++;\n"; + $output .= " {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n"; + $output .= " {$foreach_props}['last'] = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n"; + } else { + $output .= "if (count((array)$from)):\n"; + $output .= " foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n"; + } $output .= '?>'; return $output; diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index dd187920..d85a874c 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -557,17 +557,16 @@ class Smarty_Compiler extends Smarty { $this->_syntax_error("missing 'item' attribute"); } - if (empty($attrs['name'])) { - $this->_syntax_error("missing 'name' attribute"); - } - $from = $attrs['from']; $item = $this->_dequote($attrs['item']); - $name = $attrs['name']; + if (isset($attrs['name'])) + $name = $attrs['name']; $output = '_foreach[$name])) unset(\$this->_foreach[$name]);\n"; - $foreach_props = "\$this->_foreach[$name]"; + if (isset($name)) { + $output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n"; + $foreach_props = "\$this->_foreach[$name]"; + } $key_part = ''; @@ -584,14 +583,19 @@ class Smarty_Compiler extends Smarty { } } - $output .= "{$foreach_props}['total'] = count((array)$from);\n"; - $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n"; - $output .= "if ({$foreach_props}['show']):\n"; - $output .= "{$foreach_props}['iteration'] = 0;\n"; - $output .= " foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n"; - $output .= " {$foreach_props}['iteration']++;\n"; - $output .= " {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n"; - $output .= " {$foreach_props}['last'] = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n"; + if (isset($name)) { + $output .= "{$foreach_props}['total'] = count((array)$from);\n"; + $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n"; + $output .= "if ({$foreach_props}['show']):\n"; + $output .= "{$foreach_props}['iteration'] = 0;\n"; + $output .= " foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n"; + $output .= " {$foreach_props}['iteration']++;\n"; + $output .= " {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n"; + $output .= " {$foreach_props}['last'] = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n"; + } else { + $output .= "if (count((array)$from)):\n"; + $output .= " foreach ((array)$from as $key_part\$this->_tpl_vars['$item']):\n"; + } $output .= '?>'; return $output;