Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Formatting a 'GroupedBy' index value

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

Page: 1
Go to End
Author Topic: 309 Views
  • alexyoungs
    Avatar
    Community Member
    5 Posts

    Formatting a 'GroupedBy' index value Link to this post

    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

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