From 1af7561db2ac34c61b079d050cb9bacf6f9a86be Mon Sep 17 00:00:00 2001 From: messju Date: Mon, 23 Aug 2004 20:21:19 +0000 Subject: [PATCH] cleaned up attribute-handling in Smarty_Compiler::_compile_foreach_start() --- libs/Smarty_Compiler.class.php | 45 ++++++++++++++-------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index c6a5b0ec..22c39ff5 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -1122,46 +1122,39 @@ class Smarty_Compiler extends Smarty { $arg_list = array(); if (empty($attrs['from'])) { - $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__); + return $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__); } $from = $attrs['from']; if (empty($attrs['item'])) { - $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__); + return $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__); } $item = $this->_dequote($attrs['item']); if (!preg_match('!^\w+$!', $item)) { - $this->_syntax_error("'foreach: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__); + return $this->_syntax_error("'foreach: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__); } - if (isset($attrs['name'])) + if (isset($attrs['key'])) { + $key = $this->_dequote($attrs['key']); + if (!preg_match('!^\w+$!', $key)) { + return $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__); + } + $key_part = "\$this->_tpl_vars['$key'] => "; + } else { + $key = null; + $key_part = ''; + } + + if (isset($attrs['name'])) { $name = $attrs['name']; + } else { + $name = null; + } $output = '_foreach[$name])) unset(\$this->_foreach[$name]);\n"; $foreach_props = "\$this->_foreach[$name]"; - } - - $key_part = ''; - - foreach ($attrs as $attr_name => $attr_value) { - switch ($attr_name) { - case 'key': - $key = $this->_dequote($attrs['key']); - if (!preg_match('!^\w+$!', $key)) { - $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__); - } - $key_part = "\$this->_tpl_vars['$key'] => "; - break; - - case 'name': - $output .= "{$foreach_props}['$attr_name'] = $attr_value;\n"; - break; - } - } - - if (isset($name)) { + $output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n"; $output .= "{$foreach_props}['total'] = count(\$_from = (array)$from);\n"; $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n"; $output .= "if ({$foreach_props}['show']):\n";