Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » How to insert code after every third item?

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

Page: 1 2
Go to End
Author Topic: 6457 Views
  • bennettpr
    Avatar
    Community Member
    37 Posts

    Re: How to insert code after every third item? Link to this post

    Thanks Sam,

    Very helpful (and fast!)

    Paul

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: How to insert code after every third item? Link to this post

    Yeah I've hooked the forum's RSS feed into http://notify.me so that I get IM notifications of posts; it can be slightly overwhelming at times but it makes sure that I have my finger on the pulse of the forum a little more.

  • superautomatic
    Avatar
    Community Member
    53 Posts

    Re: How to insert code after every third item? Link to this post

    Thanks a lot, worked like a charm.

    To insert the clearing div after every third item i had to add 1 to iteratorPos (since it starts on 0), so my function ended up as

       function newShelf() {
       return ((($this->iteratorPos + 1) % 3) == 0);
       }

    /jens

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: How to insert code after every third item? Link to this post

    Hi, I'm trying to use this with UncleCheeses DataObjectManager to put a clearer div after every fourth iteration, but having the same trouble where inside the control loop I cant access the newShelf function. This is what I've got:

    In PeoplePage.ss

    <% if People %>
    <% control People %>   

        <div class="PersonPod">
        <div class="PersonImage">$PersonPic.SetWidth(130)</div>
    <div class="PersonName">$PersonName</div>
    <div class="PersonTitle">$PersonTitle</div>
    </div>
        <% if newShelf %>
        <div class="clearer">output this</div>
        <% end_if %>
    <% end_control %>
    <% end_if %>

    Inside the PeoplePage class:

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

    I've tried outputting $iteratorPos directly in the page template and that works, but no output of newShelf inside the control loop. Any ideas?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: How to insert code after every third item? Link to this post

    Hi Tsunami

    You need to put your newShelf() function inside your People model class. IT makes ense if you think about it because what the <% control People %> is doing is essentailly putting you in the context of those DataObjects so any functions you call from within that control need to be available from within that object.
    Just for completeness if you wanted to step out of that control block to access something on the top level page controller/model then you could use the $Top variable by calling $Top.FunctionName. As far as I can tell this doesn't work in controllers though so you couldn't do <% control top.FunctionName %>.

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: How to insert code after every third item? Link to this post

    Haha...how easy it all seems when someone points it out!

    thanks aram, that cracked it.......

  • Artyom
    Avatar
    Community Member
    22 Posts

    Re: How to insert code after every third item? Link to this post

    Can we use a similar logic for Menu iteration? I would rather not subclass the Menu, since I don't control its creation. I am making a simple footer nav, and would like to do something like

                       <% control Menu(1) %>   

                          <li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle</a>
                          <% if Iterator.peekNext %>
                          &nbsp;|&nbsp;
                          <% end_if %>
                          </li>

                       <% end_control %>

    To no avail. Also tried Children.Iterator.peekNext

    I get "Call to undefined method DataObjectSet_Iterator::cachedCall()"

    ... this is a common task, isn't it? Is there a better way?

    thanks,
    Artyom

  • dab
    Avatar
    Community Member
    50 Posts

    Re: How to insert code after every third item? Link to this post

    Artyom, i think you may use code like this:

    <% control Menu(1) %>
    <li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle</a>
    <% if Last %>
    <% else %> &nbsp;|&nbsp;
    <% end_if %>
    </li>
    <% end_control %>

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