TYPO3 API  SVNRelease
CycleViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * This ViewHelper cycles through the specified values.
00025  * This can be often used to specify CSS classes for example.
00026  *
00027  * = Examples =
00028  *
00029  * <code title="Simple">
00030  * <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>
00031  * </code>
00032  * <output>
00033  * foobarbazfoo
00034  * </output>
00035  *
00036  * <code title="Alternating CSS class">
00037  * <ul>
00038  *   <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">
00039  *     <f:cycle values="{0: 'odd', 1: 'even'}" as="zebraClass">
00040  *       <li class="{zebraClass}">{foo}</li>
00041  *     </f:cycle>
00042  *   </f:for>
00043  * </ul>
00044  * </code>
00045  * <output>
00046  * <ul>
00047  *   <li class="odd">1</li>
00048  *   <li class="even">2</li>
00049  *   <li class="odd">3</li>
00050  *   <li class="even">4</li>
00051  * </ul>
00052  * </output>
00053  *
00054  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00055  * @api
00056  */
00057 class Tx_Fluid_ViewHelpers_CycleViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00058 
00059     /**
00060      * @var array|Tx_Extbase_Persistence_ObjectStorage the values to be iterated through
00061      */
00062     protected $values = NULL;
00063 
00064     /**
00065      * @var integer current values index
00066      */
00067     protected $currentCycleIndex = NULL;
00068 
00069     /**
00070      * @param array $values The array or Tx_Extbase_Persistence_ObjectStorage to iterated over
00071      * @param string $as The name of the iteration variable
00072      * @return string Rendered result
00073      * @author Bastian Waidelich <bastian@typo3.org>
00074      * @api
00075      */
00076     public function render($values, $as) {
00077         if ($values === NULL) {
00078             return $this->renderChildren();
00079         }
00080         if ($this->values === NULL) {
00081             $this->initializeValues($values);
00082         }
00083         if ($this->currentCycleIndex === NULL || $this->currentCycleIndex >= count($this->values)) {
00084             $this->currentCycleIndex = 0;
00085         }
00086 
00087         $currentValue = isset($this->values[$this->currentCycleIndex]) ? $this->values[$this->currentCycleIndex] : NULL;
00088         $this->templateVariableContainer->add($as, $currentValue);
00089         $output = $this->renderChildren();
00090         $this->templateVariableContainer->remove($as);
00091 
00092         $this->currentCycleIndex ++;
00093 
00094         return $output;
00095     }
00096 
00097     /**
00098      * Sets this->values to the current values argument and resets $this->currentCycleIndex.
00099      *
00100      * @param array $values The array or Tx_Extbase_Persistence_ObjectStorage to be stored in $this->values
00101      * @return void
00102      * @author Bastian Waidelich <bastian@typo3.org>
00103      */
00104     protected function initializeValues($values) {
00105         if (is_object($values)) {
00106             if (!$values instanceof Traversable) {
00107                 throw new Tx_Fluid_Core_ViewHelper_Exception('CycleViewHelper only supports arrays and objects implementing Traversable interface' , 1248728393);
00108             }
00109             $this->values = iterator_to_array($values, FALSE);
00110         } else {
00111             $this->values = array_values($values);
00112         }
00113         $this->currentCycleIndex = 0;
00114     }
00115 }
00116 
00117 ?>