[TYPO3-mvc] fluid: adding own variables [discussion]

Steffen Kamper info at sk-typo3.de
Fri Jul 17 12:02:40 CEST 2009


Hi Bastian,

Bastian Waidelich schrieb:
> I've just implemented a (dummy) version of a cycle view helper:
> /**
>  *
>  * @param string $name
>  * @param array $values
>  * @return string Rendered string
>  * @author Bastian Waidelich <bastian at typo3.org>
>  * @api
>  */
> public function render($name, array $values) {
>     if 
> (!$this->viewHelperVariableContainer->exists('F3\Fluid\ViewHelpers\CycleViewHelper', 
> $name)) {
>      
> $this->viewHelperVariableContainer->add('F3\Fluid\ViewHelpers\CycleViewHelper', 
> $name, 0);
>     }
>     $currentIndex = 
> (integer)$this->viewHelperVariableContainer->get('F3\Fluid\ViewHelpers\CycleViewHelper', 
> $name);
>     if ($currentIndex >= count($values)) {
>         $currentIndex = 0;
>     }
> 
>     $this->templateVariableContainer->add($name, $values[$currentIndex]);
>     $output = $this->renderChildren();
>     $this->templateVariableContainer->remove($name);
> 
>     $currentIndex ++;
>     $this->viewHelperVariableContainer->remove('F3\Fluid\ViewHelpers\CycleViewHelper', 
> $name);
>     $this->viewHelperVariableContainer->add('F3\Fluid\ViewHelpers\CycleViewHelper', 
> $name, $currentIndex);
> 
>     return $output;
> }
> 
> 
> This works with nested loops even:
> 
> <ul>
>   <f:for each="{0 : 'a', 1 : 'a', 2 : 'b', 3 : 'b'}" as="item" key="key">
>     <f:cycle name="zebra" values="{0: 'odd', 1: 'even'}">
>       <li class="{zebra}">
>         {zebra}
>         <ul>
>           <f:for each="{0 : 'a', 1 : 'a', 2 : 'b', 3 : 'b'}" 
> as="subitem" key="subkey">
>             <li>
>               <f:cycle name="subzebra" values="{0: 'odd', 1: 'even'}">
>                 {subzebra}
>               </f:cycle>
>             </li>
>           </f:for>
>         </ul>
>       </li>
>     </f:cycle>
>   </f:for>
> </ul>
> 
> But it's just exemplary and needs better error checking
> 
> Bastian

great! :)

i wonder how it works without having $currentIndex static.

This ViewHelper should be integrated, as it has reasonable usecases :)

vg Steffen


More information about the TYPO3-project-typo3v4mvc mailing list