XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
compiler.foreachq.php
Go to the documentation of this file.
1 <?php
44 function smarty_compiler_foreachq( $argStr, &$comp ) {
45 
46  $comp->_push_tag('foreach');
47 
48  $attrs = $comp->_parse_attrs( $argStr, false );
49 
50  $arg_list = array();
51 
52  if (empty($attrs['from'])) {
53  return $comp->_syntax_error("foreachq: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
54  }
55  $from = $attrs['from'];
56 
57  if (empty($attrs['item'])) {
58  return $comp->_syntax_error("foreachq: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
59  }
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__);
63  }
64 
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__);
69  }
70  $key_part = "\$this->_tpl_vars['$key'] => ";
71  } else {
72  $key = null;
73  $key_part = '';
74  }
75 
76  if (isset($attrs['name'])) {
77  $name = $attrs['name'];
78  } else {
79  $name = null;
80  }
81 
82  $output = '';
83  //$output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
84  if (isset($name)) {
85  $foreach_props = "\$this->_foreach[$name]";
86  $output .= "{$foreach_props} = array('total' => count($from), 'iteration' => 0);\n";
87  //$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";
90  //$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
91  $output .= " {$foreach_props}['iteration']++;\n";
92  } else {
93  $output .= "if (count($from)):\n";
94  $output .= " foreach ($from as $key_part\$this->_tpl_vars['$item']):\n";
95  //$output .= "if (count(\$_from)):\n";
96  //$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
97  }
98  //$output .= '';
99 
100  return $output;
101 
102 }
103 
104 ?>