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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Rendering DataList


Go to End


3 Posts   1386 Views

Avatar
baduras

Community Member, 4 Posts

27 March 2015 at 5:11am

Hello,
I'm solving simple problem, and I need to render DataList of DataObjects. I created a template (see below). Then in my code i have $var = DataObjectXX::get()->limit(5); $html = $var->renderWith('template') //DataObjectXX has property ->a
//template
<% loop $Children %>
$a
<% end_loop %>
This is just a pseudo code, but the point is, $html does not contains rendered DataList items.
The template is rendered to frontend using Ajax call.

Can anybody help me, where could be the problem?

Thank you

Avatar
Nightjar

Community Member, 28 Posts

27 March 2015 at 6:11am

Edited: 27/03/2015 6:12am

When calling renderWith, the default scope in the template will be that object. DataList does not have a method named Children, so nothing is output.
Try <% loop $Me %>

I think a better (well, 'cleaner') way to achieve what you're trying might be:

public function ListAjaxThings() {
	return $this->customise(['TopFive'=>Thing::get()->limit(5)])->renderWith('template');
}

Where 'template' contains the familiar <% loop $TopFive %> ...

Avatar
baduras

Community Member, 4 Posts

27 March 2015 at 7:36pm

Thank you. It works