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.

Customising the CMS /

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

After extending ModelAdmin how to loop the DataObjects in the template?


Go to End


5 Posts   1399 Views

Avatar
pinkp

Community Member, 182 Posts

19 March 2015 at 4:56am

http://doc.silverstripe.org/en/developer_guides/customising_the_admin_interface/modeladmin/

So this tutorial is simple and great but seems to miss the vital ending of displaying the data on the site in your template?
The point of me using this method would be like extending SiteConfig which allows you to pull the information into any template page i.e. $SiteConfig.Title.
I want to loop the Products on any page? for example in the footer on every page.. not just a Product.ss page. I could extend SiteConfig but I want its own window / tab not to be bundled in the Settings tab.

Avatar
Pyromanik

Community Member, 419 Posts

19 March 2015 at 5:15am

Edited: 19/03/2015 5:20am

Same as any other data object collection.

http://doc.silverstripe.org/en/developer_guides/model/data_model_and_orm/#querying-data
http://doc.silverstripe.org/en/developer_guides/model/lists/#iterating-over-the-list

Make a controller function to access it.

class Page_Controller extends ContentController {
	public function Products() { return Products::get(); }
}

<% loop Products %> ... <% end_loop %>

Avatar
Devlin

Community Member, 344 Posts

19 March 2015 at 5:20am

You will need to place a method in your Page class for querying the data.

Something like:

// Page.php
class Page extends SiteTree {
	public function Products() {
		return MyDataObject::get();
	}
}
// Page.ss
<% loop $Products %>
$Title
<% end_loop %>

Avatar
pinkp

Community Member, 182 Posts

19 March 2015 at 5:43am

Great thanks! Both worked except in your example Pyromanik "return Products" needed to be "Product"

Avatar
Pyromanik

Community Member, 419 Posts

19 March 2015 at 5:48am

Yeah, it needs to match whatever your DataObject is named of course :)