1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Similar to the php foreach block, loops over an array |
---|
5 | * |
---|
6 | * Note that if you don't provide the item parameter, the key will act as item |
---|
7 | * <pre> |
---|
8 | * * from : the array that you want to iterate over |
---|
9 | * * key : variable name for the key (or for the item if item is not defined) |
---|
10 | * * item : variable name for each item |
---|
11 | * * name : foreach name to access it's iterator variables through {$.foreach.name.var} see {@link http://wiki.dwoo.org/index.php/IteratorVariables} for details |
---|
12 | * </pre> |
---|
13 | * Example : |
---|
14 | * |
---|
15 | * <code> |
---|
16 | * {foreach $array val} |
---|
17 | * {$val.something} |
---|
18 | * {/foreach} |
---|
19 | * </code> |
---|
20 | * |
---|
21 | * This software is provided 'as-is', without any express or implied warranty. |
---|
22 | * In no event will the authors be held liable for any damages arising from the use of this software. |
---|
23 | * |
---|
24 | * @author Jordi Boggiano <j.boggiano@seld.be> |
---|
25 | * @copyright Copyright (c) 2008, Jordi Boggiano |
---|
26 | * @license http://dwoo.org/LICENSE Modified BSD License |
---|
27 | * @link http://dwoo.org/ |
---|
28 | * @version 1.1.0 |
---|
29 | * @date 2009-07-18 |
---|
30 | * @package Dwoo |
---|
31 | */ |
---|
32 | class Dwoo_Plugin_foreach extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable |
---|
33 | { |
---|
34 | public static $cnt=0; |
---|
35 | |
---|
36 | public function init($from, $key=null, $item=null, $name='default', $implode=null) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) |
---|
41 | { |
---|
42 | // get block params and save the current template pointer to use it in the postProcessing method |
---|
43 | $currentBlock =& $compiler->getCurrentBlock(); |
---|
44 | $currentBlock['params']['tplPointer'] = $compiler->getPointer(); |
---|
45 | |
---|
46 | return ''; |
---|
47 | } |
---|
48 | |
---|
49 | public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) |
---|
50 | { |
---|
51 | $params = $compiler->getCompiledParams($params); |
---|
52 | $tpl = $compiler->getTemplateSource($params['tplPointer']); |
---|
53 | |
---|
54 | // assigns params |
---|
55 | $src = $params['from']; |
---|
56 | |
---|
57 | if ($params['item'] !== 'null') { |
---|
58 | if ($params['key'] !== 'null') { |
---|
59 | $key = $params['key']; |
---|
60 | } |
---|
61 | $val = $params['item']; |
---|
62 | } elseif ($params['key'] !== 'null') { |
---|
63 | $val = $params['key']; |
---|
64 | } else { |
---|
65 | throw new Dwoo_Compilation_Exception($compiler, 'Foreach <em>item</em> parameter missing'); |
---|
66 | } |
---|
67 | $name = $params['name']; |
---|
68 | |
---|
69 | if (substr($val, 0, 1) !== '"' && substr($val, 0, 1) !== '\'') { |
---|
70 | throw new Dwoo_Compilation_Exception($compiler, 'Foreach <em>item</em> parameter must be of type string'); |
---|
71 | } |
---|
72 | if (isset($key) && substr($val, 0, 1) !== '"' && substr($val, 0, 1) !== '\'') { |
---|
73 | throw new Dwoo_Compilation_Exception($compiler, 'Foreach <em>key</em> parameter must be of type string'); |
---|
74 | } |
---|
75 | |
---|
76 | // evaluates which global variables have to be computed |
---|
77 | $varName = '$dwoo.foreach.'.trim($name, '"\'').'.'; |
---|
78 | $shortVarName = '$.foreach.'.trim($name, '"\'').'.'; |
---|
79 | $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
---|
80 | $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false; |
---|
81 | $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false; |
---|
82 | $usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false; |
---|
83 | $usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false; |
---|
84 | $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false; |
---|
85 | $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false; |
---|
86 | |
---|
87 | if (strpos($name, '$this->scope[') !== false) { |
---|
88 | $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true; |
---|
89 | } |
---|
90 | |
---|
91 | // override globals vars if implode is used |
---|
92 | if ($params['implode'] !== 'null') { |
---|
93 | $implode = $params['implode']; |
---|
94 | $usesAny = true; |
---|
95 | $usesLast = true; |
---|
96 | $usesIteration = true; |
---|
97 | $usesTotal = true; |
---|
98 | } |
---|
99 | |
---|
100 | // gets foreach id |
---|
101 | $cnt = self::$cnt++; |
---|
102 | |
---|
103 | // build pre content output |
---|
104 | $pre = Dwoo_Compiler::PHP_OPEN . "\n".'$_fh'.$cnt.'_data = '.$src.';'; |
---|
105 | // adds foreach properties |
---|
106 | if ($usesAny) { |
---|
107 | $pre .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n("; |
---|
108 | if ($usesIndex) $pre .="\n\t".'"index" => 0,'; |
---|
109 | if ($usesIteration) $pre .="\n\t".'"iteration" => 1,'; |
---|
110 | if ($usesFirst) $pre .="\n\t".'"first" => null,'; |
---|
111 | if ($usesLast) $pre .="\n\t".'"last" => null,'; |
---|
112 | if ($usesShow) $pre .="\n\t".'"show" => $this->isArray($_fh'.$cnt.'_data, true),'; |
---|
113 | if ($usesTotal) $pre .="\n\t".'"total" => $this->count($_fh'.$cnt.'_data),'; |
---|
114 | $pre.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];'; |
---|
115 | } |
---|
116 | // checks if foreach must be looped |
---|
117 | $pre .= "\n".'if ($this->isTraversable($_fh'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') == true)'."\n{"; |
---|
118 | // iterates over keys |
---|
119 | $pre .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{"; |
---|
120 | // updates properties |
---|
121 | if ($usesFirst) { |
---|
122 | $pre .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);'; |
---|
123 | } |
---|
124 | if ($usesLast) { |
---|
125 | $pre .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);'; |
---|
126 | } |
---|
127 | $pre .= "\n/* -- foreach start output */\n".Dwoo_Compiler::PHP_CLOSE; |
---|
128 | |
---|
129 | // build post content output |
---|
130 | $post = Dwoo_Compiler::PHP_OPEN . "\n"; |
---|
131 | |
---|
132 | if (isset($implode)) { |
---|
133 | $post .= '/* -- implode */'."\n".'if (!$_fh'.$cnt.'_glob["last"]) {'. |
---|
134 | "\n\t".'echo '.$implode.";\n}\n"; |
---|
135 | } |
---|
136 | $post .= '/* -- foreach end output */'; |
---|
137 | // update properties |
---|
138 | if ($usesIndex) { |
---|
139 | $post.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;'; |
---|
140 | } |
---|
141 | if ($usesIteration) { |
---|
142 | $post.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;'; |
---|
143 | } |
---|
144 | // end loop |
---|
145 | $post .= "\n\t}\n}".Dwoo_Compiler::PHP_CLOSE; |
---|
146 | if (isset($params['hasElse'])) { |
---|
147 | $post .= $params['hasElse']; |
---|
148 | } |
---|
149 | |
---|
150 | return $pre . $content . $post; |
---|
151 | } |
---|
152 | } |
---|