enhanced error-reporting for {foreach}

This commit is contained in:
messju
2004-07-01 15:39:34 +00:00
parent cc08082d67
commit 8c83b9e88b
2 changed files with 12 additions and 4 deletions

2
NEWS
View File

@@ -1,3 +1,5 @@
- add error-messages when anything else than an identifier is passed
to foreach's key- or item-attribute (messju)
- fix handling of digits inside tagnames (messju) - fix handling of digits inside tagnames (messju)
- fix escaping of backslashes in Smarty_Compiler::_quote_replace() (messju) - fix escaping of backslashes in Smarty_Compiler::_quote_replace() (messju)

View File

@@ -1122,15 +1122,18 @@ class Smarty_Compiler extends Smarty {
$arg_list = array(); $arg_list = array();
if (empty($attrs['from'])) { if (empty($attrs['from'])) {
$this->_syntax_error("missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__); $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
} }
$from = $attrs['from'];
if (empty($attrs['item'])) { if (empty($attrs['item'])) {
$this->_syntax_error("missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__); $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__);
} }
$from = $attrs['from'];
$item = $this->_dequote($attrs['item']);
if (isset($attrs['name'])) if (isset($attrs['name']))
$name = $attrs['name']; $name = $attrs['name'];
@@ -1146,6 +1149,9 @@ class Smarty_Compiler extends Smarty {
switch ($attr_name) { switch ($attr_name) {
case 'key': case 'key':
$key = $this->_dequote($attrs['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'] => "; $key_part = "\$this->_tpl_vars['$key'] => ";
break; break;