Merge "[TASK] Increase version number"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / CycleViewHelper.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 * This ViewHelper cycles through the specified values.
16 * This can be often used to specify CSS classes for example.
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 Tx_Fluid_ViewHelpers_CycleViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
48
49 /**
50 * the values to be iterated through
51 * @var array|Tx_Extbase_Persistence_ObjectStorage
52 */
53 protected $values = NULL;
54
55 /**
56 * current values index
57 * @var integer
58 */
59 protected $currentCycleIndex = NULL;
60
61 /**
62 * @param array $values The array or Tx_Extbase_Persistence_ObjectStorage to iterated over
63 * @param string $as The name of the iteration variable
64 * @return string Rendered result
65 * @api
66 */
67 public function render($values, $as) {
68 if ($values === NULL) {
69 return $this->renderChildren();
70 }
71 if ($this->values === NULL) {
72 $this->initializeValues($values);
73 }
74 if ($this->currentCycleIndex === NULL || $this->currentCycleIndex >= count($this->values)) {
75 $this->currentCycleIndex = 0;
76 }
77
78 $currentValue = isset($this->values[$this->currentCycleIndex]) ? $this->values[$this->currentCycleIndex] : NULL;
79 $this->templateVariableContainer->add($as, $currentValue);
80 $output = $this->renderChildren();
81 $this->templateVariableContainer->remove($as);
82
83 $this->currentCycleIndex ++;
84
85 return $output;
86 }
87
88 /**
89 * Sets this->values to the current values argument and resets $this->currentCycleIndex.
90 *
91 * @param array $values The array or Tx_Extbase_Persistence_ObjectStorage to be stored in $this->values
92 * @return void
93 */
94 protected function initializeValues($values) {
95 if (is_object($values)) {
96 if (!$values instanceof Traversable) {
97 throw new Tx_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 ?>