0922ea48413c1a36f8bdc07b0d22dc86f87372b7
[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 * This ViewHelper cycles through the specified values.
15 * This can be often used to specify CSS classes for example.
16 * Note:** To achieve the "zebra class" effect in a loop you can also use the "iteration" argument of the **for** ViewHelper.
17 *
18 * = Examples =
19 *
20 * <code title="Simple">
21 * <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>
22 * </code>
23 * <output>
24 * foobarbazfoo
25 * </output>
26 *
27 * <code title="Alternating CSS class">
28 * <ul>
29 * <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">
30 * <f:cycle values="{0: 'odd', 1: 'even'}" as="zebraClass">
31 * <li class="{zebraClass}">{foo}</li>
32 * </f:cycle>
33 * </f:for>
34 * </ul>
35 * </code>
36 * <output>
37 * <ul>
38 * <li class="odd">1</li>
39 * <li class="even">2</li>
40 * <li class="odd">3</li>
41 * <li class="even">4</li>
42 * </ul>
43 * </output>
44 *
45 * @api
46 */
47 class CycleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
48
49 /**
50 * the values to be iterated through
51 *
52 * @var array|\TYPO3\CMS\Extbase\Persistence\ObjectStorage
53 */
54 protected $values = NULL;
55
56 /**
57 * current values index
58 *
59 * @var integer
60 */
61 protected $currentCycleIndex = NULL;
62
63 /**
64 * @param array $values The array or object implementing ArrayAccess (for example \TYPO3\CMS\Extbase\Persistence\ObjectStorage) to iterated over
65 * @param string $as The name of the iteration variable
66 * @return string Rendered result
67 * @api
68 */
69 public function render($values, $as) {
70 if ($values === NULL) {
71 return $this->renderChildren();
72 }
73 if ($this->values === NULL) {
74 $this->initializeValues($values);
75 }
76 if ($this->currentCycleIndex === NULL || $this->currentCycleIndex >= count($this->values)) {
77 $this->currentCycleIndex = 0;
78 }
79 $currentValue = isset($this->values[$this->currentCycleIndex]) ? $this->values[$this->currentCycleIndex] : NULL;
80 $this->templateVariableContainer->add($as, $currentValue);
81 $output = $this->renderChildren();
82 $this->templateVariableContainer->remove($as);
83 $this->currentCycleIndex++;
84 return $output;
85 }
86
87 /**
88 * Sets this->values to the current values argument and resets $this->currentCycleIndex.
89 *
90 * @param array $values The array or \TYPO3\CMS\Extbase\Persistence\ObjectStorage to be stored in $this->values
91 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
92 * @return void
93 */
94 protected function initializeValues($values) {
95 if (is_object($values)) {
96 if (!$values instanceof \Traversable) {
97 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('CycleViewHelper only supports arrays and objects implementing Traversable interface', 1248728393);
98 }
99 $this->values = iterator_to_array($values, FALSE);
100 } else {
101 $this->values = array_values($values);
102 }
103 $this->currentCycleIndex = 0;
104 }
105 }
106
107 ?>