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

Bastian Waidelich bastian at typo3.org
Sun Jul 18 21:44:33 CEST 2010


Stephen Bungert wrote:

Hi Stephan,

> I want to add the classes "first" or "last" to a tag in my template [...]
> I suppossed to do this kind of thing in a custom viewhelper and not in the Controller?

You should certainly not have to do this in your controller (cause 
you'll have to do it again, if you need it in another place and code 
duplication is eeevil. Apart from that it unnecessarily bloats your 
controller).
Whether iteration information should be part of the model or handled in 
the view depends on the case.

E.g. the track number of a song on the cd would most probably be stored 
in the model. But if you only want to consecutively number a list of 
items or highlight them dependent on their position, you can do this in 
the view.

For the numbering it might be enough to use an ordered list element like:

<ol>
	<f:for each="{items}" as="item">
		<li>{item}</li>
	</f:for>
</ol>

Or, you could CSS for that:

ul {
	counter-reset: item
}
li:before {
	content: counter(item) ". ";
	counter-increment: item
}

You can also use those to highlight first, last or odd/even rows:

ul li:first-child {
	//
}
ul li:last-child {
	//
}

ul li:nth-child(2n+1) {
	//
}
ul li:nth-child(2n) {
	//
}

But.. not all browsers support those dynamic CSS selectors yet. So yes, 
you might need a ViewHelper for this and I'm confident, that the for 
ViewHelper will provide this feature soon (see 
http://forge.typo3.org/issues/6149).
Tomorrow I'll try to add this extended for ViewHelper to the Incubator 
distribution, then you'll be able to use it already.

Best,
Bastian



More information about the TYPO3-project-typo3v4mvc mailing list