TYPO3  7.6
CycleViewHelper.php
Go to the documentation of this file.
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 
49 {
55  protected $values = null;
56 
62  protected $currentCycleIndex = null;
63 
70  public function render($values, $as)
71  {
72  if ($values === null) {
73  return $this->renderChildren();
74  }
75  if ($this->values === null) {
76  $this->initializeValues($values);
77  }
78  if ($this->currentCycleIndex === null || $this->currentCycleIndex >= count($this->values)) {
79  $this->currentCycleIndex = 0;
80  }
81 
82  $currentValue = isset($this->values[$this->currentCycleIndex]) ? $this->values[$this->currentCycleIndex] : null;
83  $this->templateVariableContainer->add($as, $currentValue);
84  $output = $this->renderChildren();
85  $this->templateVariableContainer->remove($as);
86 
87  $this->currentCycleIndex ++;
88 
89  return $output;
90  }
91 
99  protected function initializeValues($values)
100  {
101  if (is_object($values)) {
102  if (!$values instanceof \Traversable) {
103  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('CycleViewHelper only supports arrays and objects implementing \Traversable interface', 1248728394);
104  }
105  $this->values = iterator_to_array($values, false);
106  } else {
107  $this->values = array_values($values);
108  }
109  $this->currentCycleIndex = 0;
110  }
111 }