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 /

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

Displaying grandchildren pages in alphabetical order


Go to End
Reply


9 Posts   1493 Views

Avatar
Strandoo

Community Member, 4 Posts

26 March 2013 at 1:03am

Hi all,

I'm trying to display links to 'nephew' pages; i.e. sibling pages' children. The structure is like this:

Australia (CityHolder.ss)
---Melbourne (City.ss)
---Sydney
Austria
---Salzburg
---Vienna
Belgium
---Brussels
---Bruges
etc.

When on any page, I'd like to display a list of countries and a list of cities (in a select menu) for navigation between all pages. The countries aren't a problem (using <% loop Menu(2) %>) but looping menu(3) just gives me the few sub-pages of a given country.

I've come close by using a sibling function found here. This gives me a list of links, but they're listed by country, so it's a bit random looking:
- Melbourne
- Sydney
- Salzburg
- Vienna
- Brussels
- Bruges

Is there a way to output the list alphabetically? I'm a noob, so you may need to go in to some detail! Thanks in advance.

Avatar
Willr

Forum Moderator, 5521 Posts

26 March 2013 at 8:03pm

While inside your Loop Menu(2) use <% loop Children %> to get the children of the 2nd level. If you want to sort you can use .Sort in templates - <% loop Children.Sort(Title) %>..<% end_loop %>

Avatar
Strandoo

Community Member, 4 Posts

3 April 2013 at 1:43am

Hi Willr. Thanks for your quick reply. I've tried your suggestion but it's still not doing what I want. Using .Sort(Title) does sort the children, but only within the parent. So, while my cities are sorted within their country, I really want them sorted without reference to their country:

Current: ...Dublin, Killarney, Shannon, Florence, Milan, Rome Venice, Kyoto, Tokyo ...
What I want: ...Dublin, Florence, Killarney, Kyoto, Milan, Rome, Shannon, Tokyo, Venice, ...

The parent countries aren't listed, so I'd like to have all the cities listed as one long alphabetised list.

Any suggestions?

Avatar
Willr

Forum Moderator, 5521 Posts

3 April 2013 at 6:34pm

Then don't use nested controls, create a new function to return your sorted list

function getCities() {
return DataObject::get("City", "", "Title ASC");
}

Assuming you have a pagetype for 'City', if not, then you can use the 2nd parameter for doing a SQL 'where' query.

Avatar
Strandoo

Community Member, 4 Posts

3 April 2013 at 11:02pm

Thanks! That worked nicely. I think this is beginning to make sense to me now!

Avatar
pinkp

Community Member, 179 Posts

11 June 2015 at 9:43am

This works if you want to show ALL the grandchildren on one page but what if you want to divide the grandchildren by their parents and still sort them alphabetically? So lists separated by the child page with lists of grandchild pages sorted alphabetically.

Avatar
Pyromanik

Community Member, 355 Posts

12 June 2015 at 2:44am

<% loop $Children.Sort('Title') %>
	<h3>$Title</h3>
	<% if Children %>
		<ul>
			<% loop $Children.Sort('Title') %>
				<li>$Title</li>
			<% end_loop %>
		</ul>
	<% else %>
		<p>No children.</p>
	<% end_if %>
<% end_loop %>

Avatar
pinkp

Community Member, 179 Posts

12 June 2015 at 3:01am

Edited: 12/06/2015 4:15am

Many Thanks Pyromanik but I didn't explain well enough what I meant. I need

PAGE / TEMPLATE
Children (Listed)
Grandchildren (not shown)
Great Grandchildren Listed (not grouped by their parent in this case the Grandchildren)

If you loop Children twice it groups them. I need to bypass the grouping and take all my great grandchild pages and sort them regardless of their parent. Almost there here: http://stackoverflow.com/questions/30768447/silverstripe-3-loop-greatgrandchildren-pages-with-out-grouping-by-parent/30773439?noredirect=1#comment49604901_30773439

But now I'm stuck as I'm sorting it by another variable that is not on the Page.php (I think) and getting this error: ORDER BY "ProductReleaseDate" ASC Unknown column 'ProductReleaseDate' in 'field list'
--------------------
EDIT:

Solved in the end here: http://stackoverflow.com/questions/30768447/silverstripe-3-loop-greatgrandchildren-pages-with-out-grouping-by-parent/30773439?noredirect=1#comment49618856_30773439

Go to Top