Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Counting a controll loop

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 2112 Views
  • vegetav
    Avatar
    Community Member
    23 Posts

    Counting a controll loop Link to this post

    I know this has been discussed alot throughout the Silverstripe community but I can't seem to get it working, maybe I'm just missing something simple!

    I basically have the below code, but I wan't to add the class 'last' to every 6th item for CSS purposes.

    (ProjectHolder.ss)
    <% control Children %>
    <div class="project">
    <p>info</p>
    </div>
    <% end_control %>

    People seem to be talking about code similar to this....

    (ProjectPage.php)
    public function IsSixth(){
    return ($this->iteratorPos % 6) == 0;
    }

    But it doesn't work for me... if I do '$iteratorPos' in my .ss file, It prints the iteratorPos, but by trying a simple print_r($this->iteratorPos) in my IsSixth function it returns NULL... so I'm presuming this is the problem.

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: Counting a controll loop Link to this post

    make sure you put function IsSixth() inside the page class instead of page controller class. Like this.

    class ProjectPage extends Page
    {
    ...
    public function IsSixth(){
    return ((($this->iteratorPos+1) % 6 ) == 0 && $this->iteratorPos!=0);
    }
    ...
    }

    class ProjectPage_Controller extends Page_Controller
    {
    //not in here
    }

    take a note, the $this->iteratorPos start at 0, so if you are after the sixth element you need use ($this->iteratorPos+1). Also I assume you don't want to add the 'last' class to the first element, that's what $this->iteratorPos != 0 is for.

    In your template do the following.

    <% control Children %>
    <div class="project <% if IsSixth %>last<% end_if %>">
    <p>info</p>
    </div>
    <% end_control %>

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Counting a controll loop Link to this post

    If you're using 2.4 you can also use MultipleOf or Modulus directly in the template - http://doc.silverstripe.org/sapphire/en/reference/advanced-templates#modulus-and-multipleof

    <% if MultipleOf(6) %>sixth<% end_if %> should work.

  • vegetav
    Avatar
    Community Member
    23 Posts

    Re: Counting a controll loop Link to this post

    Thanks guys... very helpful... Ben's fix for my IsSixth function works but In the end I when for the <% if MultipleOf(6) %> option, I didn't realise that SilverStripe had that function built in

    Cheers!

    2112 Views
Page: 1
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.