Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Date pushed to array no longer formats in template

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

Page: 1
Go to End
Author Topic: 1640 Views
  • Laurie
    Avatar
    Community Member
    21 Posts

    Date pushed to array no longer formats in template Link to this post

    I have page which lists a series of publications...some of the publications link to a PDF file while others link to another HTML page (for example, if the publication has a series of volumes).

    In my controller I have the following function to grab the list of publications:

       function Publications() {
          $publications = new DataObjectSet();
          $results = DB::query("SELECT publication.*, file.Filename
             FROM publication_practice LEFT OUTER JOIN publication ON publication_practice.PublicationID = publication.ID
                LEFT OUTER JOIN file ON publication.PDFLinkID = file.ID
             WHERE publication_practice.PracticePageID = '{$this->ParentID}'
             ORDER BY Date DESC, Title ASC");
          if ($results) {
             foreach($results as $result) {
                $htmllinkID = $result['HTMLLinkID'];
                $publications -> push (new ArrayData (array (
                   'Title' => $result['Title'],
                   'Date' => $result['Date'],
                   'Filename' => $result['Filename'],
                   'Abstract' => $result['Abstract'],
                   'HTMLLink' => DataObject::get('PublicationPage', "sitetree_live.ID = '$htmllinkID'")
                )));
             }
             return $publications;
          }
          return false;   
       }

    In my template, ($Date) will output (2010-02-01) however, ($Date.Format(F Y)) outputs just () rather then (February 2010). My sense is that the date is being converted to a string when it is pushed to the array. So...is there a way to (1) push it to the array as a date, (2) format it as I'm pushing it into the array (the several things I've tried haven't worked), or (3) another way to get it formatted in the template? Thanks.

    Cheers,
    Laurie

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Date pushed to array no longer formats in template Link to this post

    When you are dealing with results like that you have simple strings, in order to use the values as objects you need to create the objects and pass them in like...

    'Date' => new Date($result['Date']);

    Or I think you can do 'Date' => DBField::create('Date', $result['Date']); Can't remember the differences.

  • Laurie
    Avatar
    Community Member
    21 Posts

    Re: Date pushed to array no longer formats in template Link to this post

    Thanks Willr!

    I tried both suggestions...the winner seems to be DBField::create.

    So in my code I switched

       $publications -> push (new ArrayData (array (
          'Title' => $result['Title'],
          'Date' => $result['Date'],
          'Filename' => $result['Filename'],
          'Abstract' => $result['Abstract'],
          'HTMLLink' => DataObject::get('PublicationPage', "sitetree_live.ID = '$htmllinkID'")
       )));

    to

       $publications -> push (new ArrayData (array (
          'Title' => $result['Title'],
          'Date' => DBField::create('Date', $result['Date']),
          'Filename' => $result['Filename'],
          'Abstract' => $result['Abstract'],
          'HTMLLink' => DataObject::get('PublicationPage', "sitetree_live.ID = '$htmllinkID'")
       )));

    And now ($Date.Format(F Y)) is working perfectly in my template.

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