TYPO3 API  SVNRelease
ForViewHelper.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  * Loop view helper which can be used to interate over array.
00025  * Implements what a basic foreach()-PHP-method does.
00026  *
00027  * = Examples =
00028  *
00029  * <code title="Simple Loop">
00030  * <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">{foo}</f:for>
00031  * </code>
00032  * <output>
00033  * 1234
00034  * </output>
00035  *
00036  * <code title="Output array key">
00037  * <ul>
00038  *   <f:for each="{fruit1: 'apple', fruit2: 'pear', fruit3: 'banana', fruit4: 'cherry'}" as="fruit" key="label">
00039  *     <li>{label}: {fruit}</li>
00040  *   </f:for>
00041  * </ul>
00042  * </code>
00043  * <output>
00044  * <ul>
00045  *   <li>fruit1: apple</li>
00046  *   <li>fruit2: pear</li>
00047  *   <li>fruit3: banana</li>
00048  *   <li>fruit4: cherry</li>
00049  * </ul>
00050  * </output>
00051  *
00052  * <code title="Iteration information">
00053  * <ul>
00054  *   <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo" iteration="fooIterator">
00055  *     <li>Index: {fooIterator.index} Cycle: {fooIterator.cycle} Total: {fooIterator.total}{f:if(condition: fooIterator.isEven, then: ' Even')}{f:if(condition: fooIterator.isOdd, then: ' Odd')}{f:if(condition: fooIterator.isFirst, then: ' First')}{f:if(condition: fooIterator.isLast, then: ' Last')}</li>
00056  *   </f:for>
00057  * </ul>
00058  * </code>
00059  * <output>
00060  * <ul>
00061  *   <li>Index: 0 Cycle: 1 Total: 4 Odd First</li>
00062  *   <li>Index: 1 Cycle: 2 Total: 4 Even</li>
00063  *   <li>Index: 2 Cycle: 3 Total: 4 Odd</li>
00064  *   <li>Index: 3 Cycle: 4 Total: 4 Even Last</li>
00065  * </ul>
00066  * </output>
00067  *
00068  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00069  * @api
00070  */
00071 class Tx_Fluid_ViewHelpers_ForViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00072 
00073     /**
00074      * Iterates through elements of $each and renders child nodes
00075      *
00076      * @param array $each The array or Tx_Extbase_Persistence_ObjectStorage to iterated over
00077      * @param string $as The name of the iteration variable
00078      * @param string $key The name of the variable to store the current array key
00079      * @param boolean $reverse If enabled, the iterator will start with the last element and proceed reversely
00080      * @param string $iteration The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)
00081      * @return string Rendered string
00082      * @author Sebastian Kurfürst <sebastian@typo3.org>
00083      * @author Bastian Waidelich <bastian@typo3.org>
00084      * @author Robert Lemke <robert@typo3.org>
00085      * @api
00086      */
00087     public function render($each, $as, $key = '', $reverse = FALSE, $iteration = NULL) {
00088         $output = '';
00089         if ($each === NULL) {
00090             return '';
00091         }
00092         if (is_object($each) && !$each instanceof Traversable) {
00093             throw new Tx_Fluid_Core_ViewHelper_Exception('ForViewHelper only supports arrays and objects implementing Traversable interface' , 1248728393);
00094         }
00095 
00096         if ($reverse === TRUE) {
00097                 // array_reverse only supports arrays
00098             if (is_object($each)) {
00099                 $each = iterator_to_array($each);
00100             }
00101             $each = array_reverse($each);
00102         }
00103         $iterationData = array(
00104             'index' => 0,
00105             'cycle' => 1,
00106             'total' => count($each)
00107         );
00108 
00109         $output = '';
00110         foreach ($each as $keyValue => $singleElement) {
00111             $this->templateVariableContainer->add($as, $singleElement);
00112             if ($key !== '') {
00113                 $this->templateVariableContainer->add($key, $keyValue);
00114             }
00115             if ($iteration !== NULL) {
00116                 $iterationData['isFirst'] = $iterationData['cycle'] === 1;
00117                 $iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
00118                 $iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
00119                 $iterationData['isOdd'] = !$iterationData['isEven'];
00120                 $this->templateVariableContainer->add($iteration, $iterationData);
00121                 $iterationData['index'] ++;
00122                 $iterationData['cycle'] ++;
00123             }
00124             $output .= $this->renderChildren();
00125             $this->templateVariableContainer->remove($as);
00126             if ($key !== '') {
00127                 $this->templateVariableContainer->remove($key);
00128             }
00129             if ($iteration !== NULL) {
00130                 $this->templateVariableContainer->remove($iteration);
00131             }
00132         }
00133         return $output;
00134     }
00135 }
00136 
00137 ?>