[TASK] Sync CMS Fluid with Flow Fluid 1.1 (part1)
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / CycleViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * This ViewHelper cycles through the specified values.
16 * This can be often used to specify CSS classes for example.
17 * **Note:** To achieve the "zebra class" effect in a loop you can also use the "iteration" argument of the **for** ViewHelper.
18 *
19 * = Examples =
20 *
21 * <code title="Simple">
22 * <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo"><f:cycle values="{0: 'foo', 1: 'bar', 2: 'baz'}" as="cycle">{cycle}</f:cycle></f:for>
23 * </code>
24 * <output>
25 * foobarbazfoo
26 * </output>
27 *
28 * <code title="Alternating CSS class">
29 * <ul>
30 * <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">
31 * <f:cycle values="{0: 'odd', 1: 'even'}" as="zebraClass">
32 * <li class="{zebraClass}">{foo}</li>
33 * </f:cycle>
34 * </f:for>
35 * </ul>
36 * </code>
37 * <output>
38 * <ul>
39 * <li class="odd">1</li>
40 * <li class="even">2</li>
41 * <li class="odd">3</li>
42 * <li class="even">4</li>
43 * </ul>
44 * </output>
45 *
46 * @api
47 */
48 class CycleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
49
50 /**
51 * the values to be iterated through
52 *
53 * @var array|\TYPO3\CMS\Extbase\Persistence\ObjectStorage
54 */
55 protected $values = NULL;
56
57 /**
58 * current values index
59 *
60 * @var integer
61 */
62 protected $currentCycleIndex = NULL;
63
64 /**
65 * @param array $values The array or object implementing \ArrayAccess (for example \TYPO3\CMS\Extbase\Persistence\ObjectStorage) to iterated over
66 * @param string $as The name of the iteration variable
67 * @return string Rendered result
68 * @api
69 */
70 public function render($values, $as) {
71 if ($values === NULL) {
72 return $this->renderChildren();
73 }
74 if ($this->values === NULL) {
75 $this->initializeValues($values);
76 }
77 if ($this->currentCycleIndex === NULL || $this->currentCycleIndex >= count($this->values)) {
78 $this->currentCycleIndex = 0;
79 }
80
81 $currentValue = isset($this->values[$this->currentCycleIndex]) ? $this->values[$this->currentCycleIndex] : NULL;
82 $this->templateVariableContainer->add($as, $currentValue);
83 $output = $this->renderChildren();
84 $this->templateVariableContainer->remove($as);
85
86 $this->currentCycleIndex ++;
87
88 return $output;
89 }
90
91 /**
92 * Sets this->values to the current values argument and resets $this->currentCycleIndex.
93 *
94 * @param array|\Traversable $values The array or \TYPO3\CMS\Extbase\Persistence\ObjectStorage to be stored in $this->values
95 * @return void
96 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
97 */
98 protected function initializeValues($values) {
99 if (is_object($values)) {
100 if (!$values instanceof \Traversable) {
101 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('CycleViewHelper only supports arrays and objects implementing \Traversable interface' , 1248728393);
102 }
103 $this->values = iterator_to_array($values, FALSE);
104 } else {
105 $this->values = array_values($values);
106 }
107 $this->currentCycleIndex = 0;
108 }
109 }
110
111 ?>