Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Is there a control to detect every nth list item

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

Page: 1 2 3
Go to End
Author Topic: 6916 Views
  • Hammy
    Avatar
    Community Member
    49 Posts

    Is there a control to detect every nth list item Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Is there a control to detect every nth list item Link to this post

    Use $Pos.

  • Hammy
    Avatar
    Community Member
    49 Posts

    Re: Is there a control to detect every nth list item Link to this post

    $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...

  • UncleCheese
    Avatar
    4085 Posts

    Re: Is there a control to detect every nth list item Link to this post

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

  • Hammy
    Avatar
    Community Member
    49 Posts

    Re: Is there a control to detect every nth list item Link to this post

    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?

  • Trym
    Avatar
    Community Member
    18 Posts

    Re: Is there a control to detect every nth list item Link to this post

    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

  • Hammy
    Avatar
    Community Member
    49 Posts

    Re: Is there a control to detect every nth list item Link to this post

    Unfortunately, when I try to use:

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

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

  • Trym
    Avatar
    Community Member
    18 Posts

    Re: Is there a control to detect every nth list item Link to this post

    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

    6916 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.