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