Jump to:

22983 Posts in 11719 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Gridfield loop and count in template

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 2
Go to End
Author Topic: 1343 Views
  • vxd
    Avatar
    Community Member
    16 Posts

    Gridfield loop and count in template Link to this post

    I have a gridfield and would like to count the objects and do something if count = 2.

    eg:

    <% loop Objects %>
    $title
    if ($count = 3)
    <br />

    <% end_loop %>

    Is the above possible?

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: Gridfield loop and count in template Link to this post

    <% if $Pos = 3 %>

    Or if you mean the actual number of items in the list, you might be able to do:

    <% if Objects.Count = 3 %>

    Haven't tested that though, no idea if it'll work. If it doesn't work, you can create a function in your controller to count them and return true/false.

  • Devlin
    Avatar
    Community Member
    186 Posts

    Re: Gridfield loop and count in template Link to this post

    What you're looking for is $Pos or $MultipleOf.

    <% loop Objects %>
    $title

    <% if Pos = 3 %>
    <br /> Position #3
    <% end_if %>

    <% if MultipleOf(3) %>
    <br /> each third iteration
    <% end_if %>

    <% end_loop %>

    http://doc.silverstripe.org/framework/en/reference/templates#looping-over-lists

  • vxd
    Avatar
    Community Member
    16 Posts

    Re: Gridfield loop and count in template Link to this post

    Thanks, just found the page too.

    How about conditional statements?

    <% if $Pos=="3" %> works

    How about => or != I get errors when I use !=

    "Sorry, there was a problem with handling your request."

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: Gridfield loop and count in template Link to this post

    There's no built-in less than/greater than, you can implement that sort of thing by creating a function in your controller. For 'not', use:

    <% if not $Pos=3 %>

    See here: http://doc.silverstripe.org/framework/en/reference/templates#conditional-logic

  • vxd
    Avatar
    Community Member
    16 Posts

    Re: Gridfield loop and count in template Link to this post

    Hi Kinglozzer,

    Do you have an example of how to write the custom functions in the controller?

  • vxd
    Avatar
    Community Member
    16 Posts

    Re: Gridfield loop and count in template Link to this post

    Controller

    function MoreThen() {
    if($pos > $value) {
    return TRUE;
    } else {
    return FALSE;
    }

    }

    Template

    If the function is outside of the loop it works and returns "1"

    $MoreThen(2,1)
    <% loop GalleryObjects %>
    <% end_loop %>


    If the function is inside of the loop if does not work. No value is returned.

    <% loop GalleryObjects %>
    $MoreThen(2,1)
    <% end_loop %>

  • vxd
    Avatar
    Community Member
    16 Posts

    Re: Gridfield loop and count in template Link to this post

    Found it: $Top.MoreThen(2,1)

    But $Pos doesn't seem to actually work.
    There are 5 items so " 2 is more than 1" should be fired off twice. NOthing is returned.

    <% if Top.MoreThen($Pos,2) %>
    2 is more than 1
    <% end_if %>

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