Jump to:

3393 Posts in 976 Topics by 852 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: 6720 Views
  • reekrd
    Avatar
    Community Member
    9 Posts

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

    Aaah. I actually tried that. However I didn't make the function public....

    As always. Thanks for your quick reply!

    Cheers
    .r

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

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

    Hey Uncle Cheese.

    I tried out your function but it doesn't work for me.
    It returns 1 on the first iteration and then 0 for every other iteration.
    I checked that iteratorPos was correct and it increments as one would expect.

    If I remove the ! so the function reads return $this->iteratorPos % 3; then it returns 0, 1, 2 and then starts over.

    Any idea about what could be wrong?

  • UncleCheese
    Avatar
    4085 Posts

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

    $this->iteratorPos % 3 will return 0 when 3 divides evenly into the value of iteratorPos, therefore, when that function returns false, or 0, you are on a Third increment of the loop, so that's why you need the !. You're looking for a false return.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

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

    I get that but I wasn't getting anything else back every third iteration with your function.

    I tried
    public function Third()
    {
    return !$this->iteratorPos % 3;
    }

    And it fired on the first iteration but never again. I rewrote it a bit and got it to work the way I wanted it to but the shortness of your function is very appealing. I had to write it like this

       public function Nth($val)
       {
          //if ($this->iteratorPos != 0)
        if(!($this->iteratorPos%$val)) {
        return true;
        }
          else {
             return false;      
          }
       }


    but that works and I can pass in any number I want instead of having it locked on one. This should open alot of doors when it comes to making more complicated markup or doing rows .

    Also one should really add ($this->iteratorPos+1)%$val) if you want it to correspond to the nth item since iteratorPos starts at 0. If someone wants to do something with the first item $First is already available.

  • NickJacobs
    Avatar
    Community Member
    144 Posts

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

    Hi, worked my way through some of those examples and couldn't any working as I needed, so thought I'd post my final solution (based on those above)...it might be useful to someone

    What I needed to do was break nav menus if they were more than 5 list items, as in the image

    so, in my Page class:

    public function SplitList($val=5) {
    return ($this->iteratorPos + 1) % $val == 0;
    }

    and in the template (navigation include)

    <ul class="navlist">
       <% control Menu(4) %>
       
          <li class="$LinkingMode"><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle.XML</a></li>

       <% if SplitList(5) %>
          </ul><ul class="navlist">
    <% end_if %>

       <% end_control %>
    </ul>

  • DigoART
    Avatar
    Community Member
    13 Posts

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

    Hi All,

    Could you help me with my code, as it doesn't work for me. It works for basic controls, however With my custom control it won't do anything.

    So I've got the Page class with Both custom control and Third() function to check for the number of element:

    Page.php:

       
    function LatestCase($num=6) {
       $news = DataObject::get_one("CaseStudyHolder");
       return ($news) ? DataObject::get("CaseStudy", "ParentID = $news->ID", "Featured DESC, Date DESC", "", $num) : false;
       }

    ...

    public function Third()
       {
       return !$this->iteratorPos % 3;
       }

    Then I've got a CaseStudyHolder class as well as CaseStudy class, both extend Page class

    In the CaseStudyHolder.ss I call a LatestCase(12) from Page Class, and need to get Nth elements inside this control.

    So,

    CaseStudyHolder.ss:

    ...

    <% control LatestCase(12) %>
    <div class="CaseBox">

    $Photo.SetSizeFixed(130x95)

    <div class="infoFrame">
    <% if Third %> $Title.XML <% end_if %>
    </div>

    </div>

    <% end_control %>

    However - it doesn't return anything at all, so basically the Title is not displayed

    Hopefully UncleCheese could help me solving this problem, as no one on the IRC couldn't .

    Cheers.

    PS: This is just an example, in real app Title would be on every item, however after each 3rd item there should be
    <div class="CleanAll">&nbps</div>

  • socks
    Avatar
    Community Member
    190 Posts

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

    There's an easy way now in 2.4… MultipleOf() : http://doc.silverstripe.org/templates

    <ul>
       <% if MultipleOf(4) %>
        <li class="fourth">bla</li>
       <% else %>
          <li>bla</li>
       <% end_if %>
    </ul>

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