1: <?php
2: /**
3: * foreachq Smarty compiler plug-in
4: *
5: * See the enclosed file LICENSE for licensing information.
6: * If you did not receive this file, get it at http://www.gnu.org/licenses/gpl-2.0.html
7: *
8: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
9: * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
10: * @author Skalpa Keo <skalpa@xoops.org>
11: * @package xos_opal
12: * @subpackage xos_opal_Smarty
13: * @since 2.0.14
14: */
15:
16: /**
17: * Quick foreach template plug-in
18: *
19: * This plug-in works as a direct replacement for the original Smarty
20: * {@link http://smarty.php.net/manual/en/language.function.foreach.php foreach} function.
21: *
22: * The difference with <var>foreach</var> is minimal in terms of functionality, but can boost your templates
23: * a lot: foreach duplicates the content of the variable that is iterated, to ensure non-array
24: * variables can be specified freely. This implementation does not do that, but as a consequence
25: * requires that the variable you specify in the <var>from</var> parameter is an array or
26: * (when using PHP5) an object. Check the difference between the code generated by foreach
27: * and foreachq to understand completely.
28: *
29: * <b>Note:</b> to use foreachq, only the opening tag has to be replaced. The closing tab still
30: * remains {/foreach}
31: *
32: * <code>
33: * // Iterate, slow version
34: * {foreach from=$array item=elt}
35: * {$elt}
36: * {/foreach}
37: * // Iterate, fast version
38: * {foreachq from=$array item=elt}
39: * {$elt}
40: * {/foreach}
41: * </code>
42: * @param $argStr
43: * @param $comp
44: * @return string
45: */
46: function smarty_compiler_foreachq($argStr, &$comp)
47: {
48: $comp->_push_tag('foreach');
49:
50: $attrs = $comp->_parse_attrs($argStr, false);
51:
52: $arg_list = array();
53:
54: if (empty($attrs['from'])) {
55: return $comp->_syntax_error("foreachq: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
56: }
57: $from = $attrs['from'];
58:
59: if (empty($attrs['item'])) {
60: return $comp->_syntax_error("foreachq: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
61: }
62: $item = $comp->_dequote($attrs['item']);
63: if (!preg_match('~^\w+$~', $item)) {
64: return $comp->_syntax_error("'foreachq: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
65: }
66:
67: if (isset($attrs['key'])) {
68: $key = $comp->_dequote($attrs['key']);
69: if (!preg_match('~^\w+$~', $key)) {
70: return $comp->_syntax_error("foreachq: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
71: }
72: $key_part = "\$this->_tpl_vars['$key'] => ";
73: } else {
74: $key = null;
75: $key_part = '';
76: }
77:
78: $name = null;
79: if (isset($attrs['name'])) {
80: $name = $attrs['name'];
81: } else {
82: }
83:
84: $output = '';
85: //$output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
86: if (isset($name)) {
87: $foreach_props = "\$this->_foreach[$name]";
88: $output .= "{$foreach_props} = array('total' => count($from), 'iteration' => 0);\n";
89: //$output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
90: $output .= "if ({$foreach_props}['total'] > 0):\n";
91: $output .= " foreach ($from as $key_part\$this->_tpl_vars['$item']):\n";
92: //$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
93: $output .= " {$foreach_props}['iteration']++;\n";
94: } else {
95: $output .= "if (count($from)):\n";
96: $output .= " foreach ($from as $key_part\$this->_tpl_vars['$item']):\n";
97: //$output .= "if (count(\$_from)):\n";
98: //$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
99: }
100:
101: //$output .= '';
102: return $output;
103: }
104: