46 $comp->_push_tag(
'foreach');
48 $attrs = $comp->_parse_attrs( $argStr,
false );
52 if (empty($attrs[
'from'])) {
53 return $comp->_syntax_error(
"foreachq: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
55 $from = $attrs[
'from'];
57 if (empty($attrs[
'item'])) {
58 return $comp->_syntax_error(
"foreachq: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
60 $item = $comp->_dequote($attrs[
'item']);
61 if (!preg_match(
'~^\w+$~', $item)) {
62 return $comp->_syntax_error(
"'foreachq: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
65 if (isset($attrs[
'key'])) {
66 $key = $comp->_dequote($attrs[
'key']);
67 if (!preg_match(
'~^\w+$~', $key)) {
68 return $comp->_syntax_error(
"foreachq: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
70 $key_part =
"\$this->_tpl_vars['$key'] => ";
76 if (isset($attrs[
'name'])) {
77 $name = $attrs[
'name'];
85 $foreach_props =
"\$this->_foreach[$name]";
86 $output .=
"{$foreach_props} = array('total' => count($from), 'iteration' => 0);\n";
88 $output .=
"if ({$foreach_props}['total'] > 0):\n";
89 $output .=
" foreach ($from as $key_part\$this->_tpl_vars['$item']):\n";
91 $output .=
" {$foreach_props}['iteration']++;\n";
93 $output .=
"if (count($from)):\n";
94 $output .=
" foreach ($from as $key_part\$this->_tpl_vars['$item']):\n";