SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Snippets - Little bits of code to make you happy

Displaying Fields From a Set of Pages on Another Page

Although ultimately requiring very little code, displaying data from a set of pages on another page can seem confusing for those new to SilverStripe. This example will display all of the Images from all the staff pages on our current page. This works by first returning all the staff pages to our template and cycling through each of them, drawing the Image for each one.

Add the following function to your current pages Controller:

public function getStaffPages(){
	return DataObject::get("StaffPage");
}

Then inside your template you can simply cycle through the results using <% control GetStaffPages %> like so:

<% control StaffPages %>
	<img src="$Image.URL" alt="A pic of $Name" />
<% end_control %>

Of course this can be taken a lot further, creating filters or sorts in the DataObject::get() call or adding conditions in the template.

Thanks to Moreno for suggesting this snippet.

Aram Balakjian avatar

Aram Balakjian

Aram is a web developer running London based agency Aab Web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.

No one has commented on this page yet.

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image Martijn van Nieuwenhoven
7 article image Darren-Lee
8 article image Roman Schmid
9 article image Matt Clegg
10 article image dalesaurus

View full leaderboard


Advertisement