Merge "[TASK] Increase version number"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / ForViewHelper.php
1 <?php
2
3 /* *
4 * This script is backported from the FLOW3 package "TYPO3.Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License, either version 3 *
8 * of the License, or (at your option) any later version. *
9 * *
10 * The TYPO3 project - inspiring people to share! *
11 * */
12
13
14 /**
15 * Loop view helper which can be used to interate over array.
16 * Implements what a basic foreach()-PHP-method does.
17 *
18 * = Examples =
19 *
20 * <code title="Simple Loop">
21 * <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">{foo}</f:for>
22 * </code>
23 * <output>
24 * 1234
25 * </output>
26 *
27 * <code title="Output array key">
28 * <ul>
29 * <f:for each="{fruit1: 'apple', fruit2: 'pear', fruit3: 'banana', fruit4: 'cherry'}" as="fruit" key="label">
30 * <li>{label}: {fruit}</li>
31 * </f:for>
32 * </ul>
33 * </code>
34 * <output>
35 * <ul>
36 * <li>fruit1: apple</li>
37 * <li>fruit2: pear</li>
38 * <li>fruit3: banana</li>
39 * <li>fruit4: cherry</li>
40 * </ul>
41 * </output>
42 *
43 * <code title="Iteration information">
44 * <ul>
45 * <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo" iteration="fooIterator">
46 * <li>Index: {fooIterator.index} Cycle: {fooIterator.cycle} Total: {fooIterator.total}{f:if(condition: fooIterator.isEven, then: ' Even')}{f:if(condition: fooIterator.isOdd, then: ' Odd')}{f:if(condition: fooIterator.isFirst, then: ' First')}{f:if(condition: fooIterator.isLast, then: ' Last')}</li>
47 * </f:for>
48 * </ul>
49 * </code>
50 * <output>
51 * <ul>
52 * <li>Index: 0 Cycle: 1 Total: 4 Odd First</li>
53 * <li>Index: 1 Cycle: 2 Total: 4 Even</li>
54 * <li>Index: 2 Cycle: 3 Total: 4 Odd</li>
55 * <li>Index: 3 Cycle: 4 Total: 4 Even Last</li>
56 * </ul>
57 * </output>
58 *
59 * @api
60 */
61 class Tx_Fluid_ViewHelpers_ForViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper implements Tx_Fluid_Core_ViewHelper_Facets_CompilableInterface {
62
63 /**
64 * Iterates through elements of $each and renders child nodes
65 *
66 * @param array $each The array or Tx_Extbase_Persistence_ObjectStorage to iterated over
67 * @param string $as The name of the iteration variable
68 * @param string $key The name of the variable to store the current array key
69 * @param boolean $reverse If enabled, the iterator will start with the last element and proceed reversely
70 * @param string $iteration The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)
71 * @return string Rendered string
72 * @api
73 */
74 public function render($each, $as, $key = '', $reverse = FALSE, $iteration = NULL) {
75 return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
76 }
77
78 /**
79 * @param array $arguments
80 * @param Closure $renderChildrenClosure
81 * @param Tx_Fluid_Core_Rendering_RenderingContextInterface $renderingContext
82 * @return string
83 */
84 static public function renderStatic(array $arguments, Closure $renderChildrenClosure, Tx_Fluid_Core_Rendering_RenderingContextInterface $renderingContext) {
85 $templateVariableContainer = $renderingContext->getTemplateVariableContainer();
86 if ($arguments['each'] === NULL) {
87 return '';
88 }
89 if (is_object($arguments['each']) && !$arguments['each'] instanceof Traversable) {
90 throw new Tx_Fluid_Core_ViewHelper_Exception('ForViewHelper only supports arrays and objects implementing Traversable interface' , 1248728393);
91 }
92
93 if ($arguments['reverse'] === TRUE) {
94 // array_reverse only supports arrays
95 if (is_object($arguments['each'])) {
96 $arguments['each'] = iterator_to_array($arguments['each']);
97 }
98 $arguments['each'] = array_reverse($arguments['each']);
99 }
100 $iterationData = array(
101 'index' => 0,
102 'cycle' => 1,
103 'total' => count($arguments['each'])
104 );
105
106 $output = '';
107 foreach ($arguments['each'] as $keyValue => $singleElement) {
108 $templateVariableContainer->add($arguments['as'], $singleElement);
109 if ($arguments['key'] !== '') {
110 $templateVariableContainer->add($arguments['key'], $keyValue);
111 }
112 if ($arguments['iteration'] !== NULL) {
113 $iterationData['isFirst'] = $iterationData['cycle'] === 1;
114 $iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
115 $iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
116 $iterationData['isOdd'] = !$iterationData['isEven'];
117 $templateVariableContainer->add($arguments['iteration'], $iterationData);
118 $iterationData['index'] ++;
119 $iterationData['cycle'] ++;
120 }
121 $output .= $renderChildrenClosure();
122 $templateVariableContainer->remove($arguments['as']);
123 if ($arguments['key'] !== '') {
124 $templateVariableContainer->remove($arguments['key']);
125 }
126 if ($arguments['iteration'] !== NULL) {
127 $templateVariableContainer->remove($arguments['iteration']);
128 }
129 }
130 return $output;
131 }
132 }
133
134 ?>