3217 Posts in 853 Topics by 812 members
| Go to End | Next > | |
| Author | Topic: | 6074 Views |
-
Is there a control to detect every nth list item

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?
-
Re: Is there a control to detect every nth list item

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...
-
Re: Is there a control to detect every nth list item

15 February 2009 at 6:01pm
function Fourth()
{
return !$this->iteratorPos % 4;
} -
Re: Is there a control to detect every nth list item

15 February 2009 at 10:28pm Last edited: 15 February 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?
-
Re: Is there a control to detect every nth list item

16 February 2009 at 9:37pm Last edited: 17 February 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
-
Re: Is there a control to detect every nth list item

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
-
Re: Is there a control to detect every nth list item

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
| 6074 Views | ||
| Go to Top | Next > |

