[TYPO3-mvc] Fluid - is there a way to check if an object is the first or last object?

Franz Koch typo3.RemoveForMessage at elements-net.de
Sun Jul 18 21:15:14 CEST 2010


Hey,

> I want to add the classes "first" or "last" to a tag in my template, how
> can I check if the current object in a for tag/loop is the last or first?
>
> I thought about doing something like this:
>
> public function listAction() {
> $objects = $this->itemRepository->findAll();
> $numOfObjects = count($objects);
>
> foreach($objects as $key => &$value) {
> $value->numOfObjects = $numOfObjects;
>
> if ($key == 0) {
> $value->position = 'first ';
>
> } else if ($key == $numOfObjects - 1) {
> $value->position = 'last ';
>
> } else {
> $value->position = '';
> }
> }
>
> $this->view->assign('items', $objects);
> }

> This works, the new properties are added and in my fluid template I just
> have to add {item.position}, or so I thought:
>
> <f:for each="{items}" as="item">
> <f:debug>{item}</f:debug>
> <f:cycle values="{0: 'odd', 1: 'even'}" as="oddEven">
> <li class="item{item.uid} {item.position}{oddEven}{item.classes}">
> </f:cycle>
>
> In the debug I see my properties, but {item.position} is always rendered
> empty, I guess because the value has to be in _cleanProperties?

nope, it has nothing to do with _cleanProperties - but you need a getter 
method for it in your object though.

But actually this should be neither part of the controller logic, nor a 
property of your object. This should move to a custom viewHelper like 
you asked in your other question.

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list