ForViewHelper

Eigener ViewHelper für Loop mit limitierter Anzahl von Einträgen

Ein ViewHelper um eine limitierte Anzahl von Einträgen zu zeigen.

Aufruf

<rsys:for start="0" until="3" array="{products}" arrayValue="product">
  {product.title}<br />
</rsys:for>

Listing

<?php

/*                                                                        *
 * This script belongs to the FLOW3 package "Fluid".                      *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation, either version 3 of the License, or (at your *
 * option) any later version.                                             *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
 * General Public License for more details.                               *
 *                                                                        *
 * You should have received a copy of the GNU Lesser General Public       *
 * License along with the script.                                         *
 * If not, see www.gnu.org/licenses/lgpl.html                    &nbsp; *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

/**
 *
 * = Examples =
 *
 * <code title="default notation">
 * {namespace rsys=Tx_Rsysproductbase_ViewHelpers}
 * <rsys:for start="0" until="3" array="{products}" arrayValue="product">
 *      {product.title}<br />
 *  </rsys:for>
 * </code>
 * <output>
 * looped array or object
 * </output>
 *
 * @license www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
 * @api
 */
class Tx_Rsysproductbase_ViewHelpers_ForViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {


    /**
     * for helper
     *
     * @param integer $start
     * @param integer $until
     * @param string $as
     * @param array $array
     * @param string $arrayValue
     *
     * @return string
     */
    public function render($start,$until,$as = null,$array = null,$arrayValue = null) {
        $returnValue = '';
        if (is_object($array)) {
            if (!$array instanceof Traversable) {
                throw new Tx_Fluid_Core_ViewHelper_Exception('ForViewHelper only supports arrays and objects implementing Traversable interface' , 1248728393);
            }
            $array = $this->convertToArray($array);
        }
        for($i = (int)$start;$i<(int)$until;$i++) {
            if($array !== null && $arrayValue !== null && count($array) > $i)
            $this->templateVariableContainer->add($arrayValue, $array[$i]);
            elseif($arrayValue !== null)
            $this->templateVariableContainer->add($arrayValue, null);
            if($as !== null)
            $this->templateVariableContainer->add($as, $i);

            $returnValue .= $this->renderChildren();

            if($arrayValue !== null)
            $this->templateVariableContainer->remove($arrayValue);
            if($as !== null)
            $this->templateVariableContainer->remove($as);
        }
        return $returnValue;
    }


    /**
     * copied from class Tx_Fluid_ViewHelpers_ForViewHelper
     *
     * Turns the given object into an array.
     * The object has to implement the Traversable interface
     *
     * @param Traversable $object The object to be turned into an array. If the object implements Iterator the key will be preserved.
     * @return array The resulting array
     * @author Bastian Waidelich <bastian@typo3.org>
     */
    protected function convertToArray(Traversable $object) {
        $array = array();
        foreach ($object as $keyValue => $singleElement) {
            //$array[$keyValue] = $singleElement;
            $array[] = $singleElement;
        }
        return $array;
    }

}

Erstellt: 09/2012| Geändert: 10/2015