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

Bastian Waidelich bastian at typo3.org
Fri Jul 17 11:33:04 CEST 2009


Christian Zenker wrote:

Hi Christian,

>> Furthermore your example get's considerably more complex by adding the 
>> zebra classes.
> 
> I just like the most flexible solution :)

Me too ;) But I prefer to have several specific view helpers for one 
task than some kind of super-joker like <f:php eval="exec('evil')" /> *g


>> <f:cycle name="zebra" values="{0: 'odd', 1: 'even'}">
>>    <li class="{zebra}">foo</li>
>> </f:cycle>
> 
> How is this supposed to work inside a ForViewHelper?

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


More information about the TYPO3-project-typo3v4mvc mailing list