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.

Template Questions

How to get data from DataObject into template


8 Posts   2675 Views


29 November 2011 at 10:23pm (Last edited: 29 November 2011 10:26pm), Community Member, 8 Posts

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'];
return array();

And also template placed in

<% control event %>
<% end_control %>

I am not able to get result from DataObject::get_by_id("FacebookEvent", $id); to template
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.


30 November 2011 at 12:12am Forum Moderator, 1091 Posts

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 (, as they give you a more thorough insight in the way SilverStripe manages relations...



30 November 2011 at 1:53am Community Member, 8 Posts

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.


30 November 2011 at 2:29am Forum Moderator, 1091 Posts

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


30 November 2011 at 2:37am (Last edited: 30 November 2011 9:14am), Community Member, 8 Posts

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.


30 November 2011 at 9:25am Community Member, 8 Posts

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.



1 December 2011 at 2:15am Community Member, 8 Posts


I found sollution


1 December 2011 at 2:34am Forum Moderator, 1091 Posts

Yes, that's a good one! Good luck :-)