Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Get values from DataList in template

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

Page: 1
Go to End
Author Topic: 522 Views
  • _Matt
    Avatar
    Community Member
    24 Posts

    Get values from DataList in template Link to this post

    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 ;)

  • _Matt
    Avatar
    Community Member
    24 Posts

    Re: Get values from DataList in template Link to this post

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

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