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

Date pushed to array no longer formats in template


Go to End


3 Posts   2768 Views

Avatar
Laurie

Community Member, 21 Posts

15 July 2010 at 8:59am

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

Avatar
Willr

Forum Moderator, 5523 Posts

15 July 2010 at 11:28am

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.

Avatar
Laurie

Community Member, 21 Posts

16 July 2010 at 1:29am

Edited: 16/07/2010 1:31am

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.