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

Formatting a 'GroupedBy' index value


Go to End


1230 Views

Avatar
alexyoungs

Community Member, 5 Posts

27 June 2013 at 12:40am

Hi all,

I'm using the GroupedBy method in the templates to group a set of data by a Y-m-d formatted date. All works fine, except that I'd like to further format the index value. For example, here's how I'm using it currently:

<% loop GroupedData.GroupedBy(Day) %>
	<h2>$Day</h2>
	<% loop Children %>
		...
	<% end_loop %>
<% end_loop %>

Where (Day) relates to the following function:

public function getDay() {
	return date('Y-m-d',strtotime($this->ActivityStartDateTime));
}

Obviously the title then renders as a Y-m-d date (i.e. 2013-06-26), but what I'd like to do is to manipulate the $Day index value, so for example, what I'm looking to achieve is something similar to:

<% loop GroupedData.GroupedBy(Day) %>
	<h2>
		<span class="calendar-icon">
			<span class="month">$Day.FormatMonth</span>
			<span class="day">$Day.FormatDay</span>
		</span>
		$Day
	</h2>
	<% loop Children %>
		...
	<% end_loop %>
<% end_loop %>

... and use a function somewhere to format the $Day value:

public function FormatMonth() {
	return date('m',strtotime($this));
}
public function FormatDay() {
	return date('d',strtotime($this));
}

If there's a way to achieve the above then I'd be grateful for some pointers from someone.

Cheers,

Alex