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.

Data Model Questions /

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

Get values from DataList in template


Go to End


2 Posts   1894 Views

Avatar
_Matt

Community Member, 29 Posts

27 February 2013 at 9:46am

Edited: 27/02/2013 9:48am

I'm learning about SS by creating a Latest Blog Posts widget. I've managed to create a function that outputs the posts and filters them by category (I'm using the blog and blogcategories modules).

What I'd like to be able to do now is output the category name in my template, but I can't figure out how. This is the function that gets the blog posts:

public function getLatestPosts()
{
    $limit = $this->NumberToShow;
    return DataList::create('BlogEntry')
    ->limit($limit)
    ->sort(array('Date' => 'DESC'))
    ->leftJoin('BlogEntry_BlogCategories', '"BlogEntry_BlogCategories"."BlogEntryID" = "BlogEntry"."ID"', null)
    ->leftJoin('BlogCategory', '"BlogCategory"."ID" = "BlogEntry_BlogCategories"."BlogCategoryID"', null)
    ->where('BlogCategoryID = ' . $this->BlogCategory);
}

Presumably the DataList contains all the data, but how to 'get it out' is eluding me.

In my template I have my control, where I'm able to get the title of the blog post, the date, content and so on:

<% control LatestPosts %>
$Date.Long
$Title
$Content.FirstParagraph()
<% end_control %>

Apologies if this is SS 101, I'm just a poor lowly frontend designer ;)

Avatar
_Matt

Community Member, 29 Posts

28 February 2013 at 11:04am

So I managed to get something working. No doubt there's a much more efficient/better way of doing it, but I'm just chuffed I got this far! However, if anyone has any suggestions on improvement they would be gratefully received.

I ended up creating separate functions. One to get the posts and one to get the category. Here are the functions:

/**
 * Get blog posts and filter them by category
 * @return DataList
 */
public function getLatestPosts()
{
    $blogCat = $this->BlogCategory;
    $limit = $this->NumberToShow;

    return DataList::create('BlogEntry')
    ->limit($limit)
    ->sort(array('Date' => 'DESC'))
    ->leftJoin('BlogEntry_BlogCategories', '"BlogEntry_BlogCategories"."BlogEntryID" = "BlogEntry"."ID"', null)
    ->leftJoin('BlogCategory', '"BlogCategory"."ID" = "BlogEntry_BlogCategories"."BlogCategoryID"', null)
    ->where('BlogCategory.ID = ' . $blogCat);
}

/**
 * Get blog post category
 * @return ArrayList
 */
public function getPostCategory()
{
    $cat = $this->BlogCategory;
    $set = new ArrayList;
    foreach(BlogCategory::get()
        ->leftJoin('BlogEntry_BlogCategories', '"BlogEntry_BlogCategories"."BlogCategoryID" = "BlogCategory"."ID"', null)
        ->where('BlogCategoryID = ' . $cat) as $obj)
    //$set->push($obj);
    $set->push($obj);
    return $set;
}

And in the template:

<% control PostCategory %>
$Title
<% end_control %>

<% control LatestPosts %>
$Date.Long
$Title
$Content.FirstParagraph()
<% end_control %>

The only thing that's slightly perplexing me is the fact that I don't seem to be able to nest my PostCategory control in my LatestPosts control?...