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 /

Displaying grandchildren pages in alphabetical order


Reply


5 Posts   831 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 [url=https://github.com/mattclegg/silverstripe-doc-restructuring/blob/master/input/recipes/siblings.txt] here[/url]. 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, 5513 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, 5513 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!