Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Counter on control loop with group by


Go to End


2 Posts   2709 Views

Avatar
stellalie

Community Member, 10 Posts

11 October 2011 at 12:51am

Edited: 23/07/2012 2:59pm

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.

Avatar
martimiz

Forum Moderator, 1391 Posts

11 October 2011 at 6:40am

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 %>