Jump to:

3447 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Accessing sub array in template

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

Page: 1
Go to End
Author Topic: 67 Views
  • macka
    Community Member
    24 Posts

    Accessing sub array in template Link to this post

    Hi Guys,

    I have an array i am passing to the template that looks like...
    bookdates =
    0 = 2014-08-26
    1 = 2014-08-27

    On my template i have..
    <% loop $itemBookedOutDates %> $bookdates <% end_loop %>
    but i can't seem to access the sub portions of the bookdates array? Normally i think it's would be something like print_r(bookdates[0]) etc to access that portion, but how do you do that in the template?


  • macka
    Community Member
    24 Posts

    Re: Accessing sub array in template Link to this post

    So what i have - is a function to get the dates between ranges and returns them as an array.

    I then wanted to put those dates into a bit of javascript on my template page.

    Just can't seem to find a way to get the code to put the values on the page. Any help would be great thanks!

    What i have so far...

       public function getItemBookedOutDates() {
          $Params = $this->getURLParams();
          $itemDatesBookedOutOn = new item_booking();
    $itemDatesBookedOutOn = item_Booking::get()->filter(array(
                'Item' => $Params['ID']

          $DateList = new ArrayList();   
          foreach($itemDatesBookedOutOn as $item) {      

             $days = $this->createDateRangeArray($item->itemBookingFrom, $item->itemBookingTo);
             $item->DateUnavailable = $days;

          return $this->DateList;
       function createDateRangeArray($strDateFrom,$strDateTo)
          // takes two dates formatted as YYYY-MM-DD and creates an
          // inclusive array of the dates between the from and to dates.

          // could test validity of dates here but I'm already doing
          // that in the main script      

          $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2), substr($strDateFrom,0,4));
          $iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2), substr($strDateTo,0,4));
          if ($iDateTo >= $iDateFrom)
             array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
             while ($iDateFrom < $iDateTo)
                $iDateFrom += 86400; // add 24 hours
          return $aryRange;

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.