Es funktioniert! Das is ja mal richtig cool! 😃
Das hab ich gemacht (Fluid Version 1.0.2 voraussgesetzt)
Den Ordner "my_extension/Classes/ViewHelpers" anlegen
Im Verzeichnis "ViewHelpers" die Datei "ForViewHelper.php" anlegen
Den Inhalt aus dem Fluid ViewHelper "ForViewHelper.php" kopieren und "iteration"-Funktionalität aus dem ViewHelper aus Fluid 1.3 übernehmen
<?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 http://www.gnu.org/licenses/lgpl.html *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* Loop view helper which can be used to interate over array.
* Implements what a basic foreach()-PHP-method does.
*
* = Examples =
*
* <code title="Simple Loop">
* <f:for each="{0:1, 1:2, 2:3, 3:4}" as="foo">{foo}</f:for>
* </code>
* <output>
* 1234
* </output>
*
* <code title="Output array key">
* <ul>
* <f:for each="{fruit1: 'apple', fruit2: 'pear', fruit3: 'banana', fruit4: 'cherry'}" as="fruit" key="label">
* <li>{label}: {fruit}</li>
* </f:for>
* </ul>
* </code>
* <output>
* <ul>
* <li>fruit1: apple</li>
* <li>fruit2: pear</li>
* <li>fruit3: banana</li>
* <li>fruit4: cherry</li>
* </ul>
* </output>
*
* @version $Id: ForViewHelper.php 1734 2009-11-25 21:53:57Z stucki $
* @package Fluid
* @subpackage ViewHelpers
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
* @api
* @scope prototype
*/
class Tx_MyExtension_ViewHelpers_ForViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
/**
* Iterates through elements of $each and renders child nodes
*
* @param array $each The array or SplObjectStorage to iterated over
* @param string $as The name of the iteration variable
* @param string $key The name of the variable to store the current array key
* @param boolean $reverse If enabled, the iterator will start with the last element and proceed reversely
* @param string $iteration The name of the variable to store iteration information (index, cycle, isFirst, isLast, isEven, isOdd)
* @return string Rendered string
* @author Sebastian Kurfürst <sebastian@typo3.org>
* @author Bastian Waidelich <bastian@typo3.org>
* @author Robert Lemke <robert@typo3.org>
* @api
*/
public function render($each, $as, $key = '', $reverse = FALSE, $iteration = NULL) {
$output = '';
if ($each === NULL) {
return '';
}
if (is_object($each)) {
if (!$each instanceof Traversable) {
throw new Tx_Fluid_Core_ViewHelper_Exception('ForViewHelper only supports arrays and objects implementing Traversable interface' , 1248728393);
}
$each = $this->convertToArray($each);
}
if ($reverse === TRUE) {
$each = array_reverse($each);
}
$iterationData = array(
'index' => 0,
'cycle' => 1,
'total' => count($each)
);
$output = '';
foreach ($each as $keyValue => $singleElement) {
$this->templateVariableContainer->add($as, $singleElement);
if ($key !== '') {
$this->templateVariableContainer->add($key, $keyValue);
}
if ($iteration !== NULL) {
$iterationData['isFirst'] = $iterationData['cycle'] === 1;
$iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
$iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
$iterationData['isOdd'] = !$iterationData['isEven'];
$this->templateVariableContainer->add($iteration, $iterationData);
$iterationData['index'] ++;
$iterationData['cycle'] ++;
}
$output .= $this->renderChildren();
$this->templateVariableContainer->remove($as);
if ($key !== '') {
$this->templateVariableContainer->remove($key);
}
if ($iteration !== NULL) {
$this->templateVariableContainer->remove($iteration);
}
}
return $output;
}
/**
* 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;
}
return $array;
}
}
?>
Im View (z.B: list.html) ganz oben folgende Zeile für den neuen Namespace einfügen
{namespace myext=Tx_MyExtension_ViewHelpers}
Dann kann man im View den eigenen ViewHelper wie folgt nutzen
<myext:for each="{objects}" as="obj" iteration="objIteration">
<p>{objIteration.cycle}</p>
</myext:for>
🙂