Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » How to get data from DataObject into template

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

Page: 1
Go to End
Author Topic: 2613 Views
  • coffeespoon
    Avatar
    Community Member
    8 Posts

    How to get data from DataObject into template Link to this post

    Hi all,

    I am really surprised by Silverstripe CMS. I decided to re-code my actual web. I've gone mad from two cases which I am not able to solve (I am sure there is not serious problem) by my very small knowlage of SS.

    Be sure I tried to search the doc first, but - no answer on my questions. I am sure I overlooked somethink...

    1. there is custom method on controller which calls method from page object

    class FacebookEventPage extends Page {
    .....
    public function getFbEvent($id) {
    $event = DataObject::get_by_id("FacebookEvent", $id);
    return $event;
    }
    ..
    }
    class FacebookEventPage_Controller extends Page_Controller {
    ...
    public function show() {
    $id = Controller::curr()->urlParams['ID'];
    $this->getFbEvent($id);
    return array();
    }


    And also template placed in FacebookEventPage_show.ss

    ...
    <% control event %>
    $event.Begin
    <% end_control %>
    ...

    I am not able to get result from DataObject::get_by_id("FacebookEvent", $id); to template ..._show.ss
    I am sure that function getFbEvent returns some data.

    And second question - how to limit (by condition) resultset when DataObject is initiated. I would tried this in __construct method, but Iam sure how.

    Many thanks for response and for really very good work you devs made on SilerStripe framework.

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: How to get data from DataObject into template Link to this post

    Hi Coffeespoon, welcome to the forums

    Your code is a bit incomplete, so I'm not sure what you're tying to accomplish... So first: do you mean to create a pagetype that can show a couple of facebook events per page, or should every page just represent one event?

    As for limiting the number of DataObjects returned from a query: you cannot define that in your DataObject, as a DataObject itself represents just one DataObject, one record. Typically you would use the $limit parameter in the DataObject::get() function.

    If you haven't yet, I'd really suggest you follow the tutorials (http://doc.silverstripe.org/sapphire/en/tutorials/), as they give you a more thorough insight in the way SilverStripe manages relations...

    Martine

  • coffeespoon
    Avatar
    Community Member
    8 Posts

    Re: How to get data from DataObject into template Link to this post

    Hallo martimiz,

    thanks for so quick response.

    The first you mentioned is right for me. ...pagetype that can show a couple of facebook events per page...
    I have one page FacebookEventPage and there is one_to_many relation. Should I send some more lines of code - which.

    to limits: sorry with dataobject - you are absolutely right. My problem is, that I would like to show only future events. Is it possible to help where in controller (I guess so) should be this default limitation implemented.

    Tutorials are my the best friends in these days ) also imagegallery, eventcallendar modules are very good friends for inspiration. I tired to reuse pieces of code without success.

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: How to get data from DataObject into template Link to this post

    OK, there are more then one ways to do this. One would be to have a EventPage that would just show an event, Then have an EventHolder page that shows all underlying eventpages in some form. For that you could follow the Article/ArticleHolder tutorial in http://doc.silverstripe.org/sapphire/en/tutorials/2-extending-a-basic-site. Also there is an Events module somewhere...

    But if you want to use DataObjects for events, you could create a 'FacebookEvent' object first, then have your FacebookEventPage 'has_many' FacebookEvents and your FacbookEvent 'has_one' FacebookEventsPages.

    Now in your template you need only:

    <% control FacebookEvents %>
       $Title - $EventDate.Nice
       ...
    <% end_control %>

    SilverStripe will automatically return the complete set of FacebookEvents. If you want to restrict, you could add a fuction like this to your page controller (simplified), which the template now will automatically call:

    function getFacebookEvents() {
       return DataObject::get('FacebookEvent', 'CURDATE() >= EventDate', 'EventDate DESC');
    }

    Something like that

  • coffeespoon
    Avatar
    Community Member
    8 Posts

    Re: How to get data from DataObject into template Link to this post

    Thank you for your time. I'll go thru your help.

    What I missed is behaviour of Holder. I misunderstood that logic. It is necssery to read more carefully.

  • coffeespoon
    Avatar
    Community Member
    8 Posts

    Re: How to get data from DataObject into template Link to this post

    Hi martimiz,

    these basics you wrote are OK. I am able to get list of all events. But I would like to click one event for detail and that is the problem for me.
    How to implement the detail. I will get detail of one event and how to move the result set to "show" template.

    Thanks

  • coffeespoon
    Avatar
    Community Member
    8 Posts
  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: How to get data from DataObject into template Link to this post

    Yes, that's a good one! Good luck

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