Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Counter on control loop with group by

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

Page: 1
Go to End
Author Topic: 1174 Views
  • stellalie
    Avatar
    Community Member
    10 Posts

    Counter on control loop with group by Link to this post

    I'm new to Silverstripe. I am bit struggling with its templating scheme.

    Basically this code:

    <% control Page %>
    <% control Children.GroupBy(Category) %>
    $PageName . " - cat " . $Category . " - " . $Counter
    <% end_control %>
    <% end_control %>

    If we have 6 page, 2 category (3 each). Then with function $Counter { return $this->iteratorPos } on page controller. Above code will ouput:

    Page 1 - cat 1 - 1
    Page 2 - cat 1 - 2
    Page 3 - cat 1 - 3
    Page 4 - cat 2 - 1
    Page 5 - cat 2 - 2
    Page 6 - cat 2 - 3

    The counter takes into account the 'Group By' on control. Is there anyway to bypass this?

    I would like a counter that returns 1 to 6 instead.

    Thanks in advance.

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Counter on control loop with group by Link to this post

    Hi there

    Normally you wouldn't have to <% control Page %>, as you're already on the page...

    Also the functiondescription says that the GroupBy(Category) will return 2 separate DataObjectSets - 1 for each Category, and I guess IteratorPos can only return the objects position within the DataObjectSet that is currently looped.

    If it's only about sorting the Childpages by category you could do something like (simplyfied, needs errorhandling, in your page controler):

    function MyChildren() {
       $children = $this->Children();
       $children->sort('Category');
       return $children;
    }

    and then

    <% control MyChildren %>
    $Pos - $Title - cat: $Category<br />
    <% end_control %>

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