Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Is there a control to detect every nth list item


Go to End


23 Posts   11063 Views

Avatar
Hammy

Community Member, 49 Posts

15 February 2009 at 4:12pm

This is probably very trival, but I need to add a class to every 4th item in a control list. For example:

<ul>
    <% control ItemList %>
        <% if nth %>
            <li class="clear-left">
        <% else %>
            <li>
        <% if_end %>
        ...
    <% end_control %>
</ul>

So for every 4th list item, what should nth be?

Is there a built in page control for this that I have missed or do I need to do something else?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

15 February 2009 at 4:49pm

Use $Pos.

Avatar
Hammy

Community Member, 49 Posts

15 February 2009 at 5:55pm

$Pos will return a counter starting at 1.

How do I use $Pos in a template list control to find out it is every nth item?

So in my example, every 4th item in a list is to have a class - ie 0,4,8,12,16,20,24,28...

Avatar
UncleCheese

Forum Moderator, 4102 Posts

15 February 2009 at 6:01pm

function Fourth()
{
return !$this->iteratorPos % 4;
}

Avatar
Hammy

Community Member, 49 Posts

15 February 2009 at 10:28pm

Edited: 15/02/2009 10:31pm

Ok, i've gotten myself a little lost here... I've added the following

class Page_Controller extends ContentController {
...
	function IsFourthItem() {
		return !$this->iteratorPos % 4; 
	}
...
}

Is this the right place to put the function?

The following code is in the template file that generates the list...

<ul>
	<% control ItemList %> 
		<% if IsFourthItem %>
			<li class="clear-left">
		<% else %>
			<li>
		<% end_if %>
	...
	<% end_control %>
</ul>

However this does not do anything but add <li> to all list items?

Avatar
Trym

Community Member, 18 Posts

16 February 2009 at 9:37pm

Edited: 17/02/2009 2:51am

Hi Hammy

Broken suggestion - This doesn't work because '%' is a reserved character I guess.

In your template file you can write:
<% control ItemList %>
<% if (($Pos % 4) == 0) %>
<li class="clear-left">
<% else %>
<li>
<% end_if %>
...
<% end_control %>

Best regards Trym

Avatar
Hammy

Community Member, 49 Posts

16 February 2009 at 10:00pm

Unfortunately, when I try to use:

<% if (($Pos % 4) == 0) %> 

this is creates a "Parse error:" error when I flush the page :(

Avatar
Trym

Community Member, 18 Posts

17 February 2009 at 2:49am

Hi Hammy

Because your code is inside at <% control ... %> element only functions in the DataObjectSet class can be used or you have to refer to $Top. The easiest way I found was to add the following function to the ViewableData.php file close to e.g. function EvenOdd().

function isForth() {
return ($this->iteratorPos + $startIndex) % 4 == 0;
}

And then in your template file write <% if isForth %>...

Best regards Trym

Go to Top