[TYPO3-mvc] ViewHelpers Setting/Updating variables restriction

Franz Koch typo3.RemoveForMessage at elements-net.de
Tue Oct 19 19:23:08 CEST 2010


Hey,

> I dislike the restriction, that you can't set variables in the template and check them again.
> If I iterate over an array (or an array-like structure), I would like to mark the first entry (which does not necessarily has the key 0) with an extra css class name.
> I couldn't find any way doing this with fluid, except modifying the array within the controller (which violates the separation of Controller and View) or writing an own ViewHelper. Is there any better/easier way?
> This seems to be a bit complex for such a simple operation. In Smarty this is just done with setting a variable to false before the loop, checking it within the loop and setting it to true...

<ul>
<f:for each="{yourArrayLikeObject}" as="item" iteration="itemIterator">
   <f:cycle values="{0:'odd',1:'even'}" as="oddEven">
	<li class="{f:if(condition:'{itemIterator.index}=0', then:'first 
')}{oddEven}">{item.name}</li>
   </f:cycle>
</f:for>
</ul>


That's a approach working with current SVN version - not sure if 4.3+ 
version already supports the "iteration" argument in the for viewHelper. 
Personally I created a modified cycle viewHelper which also accepts 
parameters for the "first" and "last" item that get added to the cycled 
values.


> I also couldn't find a way to call sections recursively. I have a tree-structure as an array.
> Each array entry has a key "name" and can have a key "children" with children for this entry.
> I thought of defining a section "renderTree" and call it recursively:
> <f:section name="renderTree">
>    <ul>
>    <f:for each="tree" as="entry">
>      <li>{entry.name}
>      <f:if "{entry.children}">
>        <f:then>
>          <f:alias map="{tree: entry.children}">
>            <f:render section="renderTree" />
>          </f:alias>
>        </f:then>
>      </f:if>
>      </li>
>    </f:for>
>    </ul>
> </f:section>
>
> <!-- controller only set the variable $tree -->
> <f:render section="renderTree" />

don't use a section, but move that stuff in a partial and nest partials. 
That's what they are for.


> Is there any way to write comments, that do not appear in the rendered source?
> I like to comment my templates, but I do not like it when all visitors of my page can see them.
> That's why I wrote a CommentViewHelper, which does not render it's children. But it is a lot of code to write for simple comments:
> <v:comment>This comment will not appear in the HTML-Source</v:comment>
>
> In Smarty:
> {* This comment will not appear in the HTML-Source *}
>
> Well, it is still possible to name the helper just CViewHelper to get<v:c>  tags...
> But anything built-in would have been nice :)

AFAIK there's currently no other way then yours to do this - but I might 
be wrong. If there's one, it'll for sure be more code to write then in 
smarty because Fluid has a parsable a XML syntax and thus comments need 
to be in a tag-like structure, like regular HTML/XML comments <!-- 
comment -->.

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list