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

List of child pages in alphabetical order


Go to End


9 Posts   6697 Views

Avatar
DeklinKelly

Community Member, 197 Posts

7 October 2010 at 8:17am

I use the code below in my template to show a list of all child pages.

Links are returned in the order of the menu hierarchy however I want the links to be returned in alphabetical order.

	<ul id="SectionPageList">
  	<% control Menu(2) %>
		<% if Children %><% else %><li><a href="$Link" title="Go to the $Title.XML page">$MenuTitle.XML</a></li><% end_if %>
		 <% if Children %> 
		 <% control Children %> 
		 <li><a href="$Link" title="Go to the $Title.XML page">$MenuTitle.XML</a></li>
		 <% end_control %> 
		 <% end_if %> 
	<% end_control %>
	</ul>

Avatar
swaiba

Forum Moderator, 1899 Posts

7 October 2010 at 8:24am

create a method SortedChildren in your controller and...

function SortedChildren()
{
	$dosChildren = $this->Children();
	if ($dosChildren)
		return $dosChildren->sort('Title',ASC);
}

Avatar
DeklinKelly

Community Member, 197 Posts

7 October 2010 at 8:41am

Thanks, now how can I use that in my template? This does not work:

Code:

class SectionPage_Controller extends Page_Controller {

	function SortedChildren () 
	{ 
	   $dosChildren = $this->Children(); 
	   if ($dosChildren) 
	      return $dosChildren->sort('Title',ASC); 
	}
}

Template:

   <ul id="SectionPageList"> 
   <% control SortedChildren %> 
   <% control Menu(2) %> 
      <% if Children %><% else %><li><a href="$Link" title="Go to the $Title.XML page">$MenuTitle.XML</a></li><% end_if %> 
       <% if Children %> 
       <% control Children %> 
       <li><a href="$Link" title="Go to the $Title.XML page">$MenuTitle.XML</a></li> 
       <% end_control %> 
       <% end_if %> 
   <% end_control %> 
   <% end_control %> 
   </ul>

Avatar
swaiba

Forum Moderator, 1899 Posts

7 October 2010 at 9:12am

any more info than, "doesn't work"?
maybe a debug::show within the function to dee a) if it is called and b) what data is flowing through it would give you an answer?

Avatar
DeklinKelly

Community Member, 197 Posts

7 October 2010 at 10:02am

Thanks. Now I get an error when I use sort:

Uncaught Exception: Object->__call(): the method 'sort' does not exist on 'SectionPage_Controller'

	function SortedChildren() { 
	$o = $this; 
	  if (!empty($o)) {
		$o->sort('Title');
		return $o;
	  }
	}

Avatar
DeklinKelly

Community Member, 197 Posts

7 October 2010 at 10:06am

Here is the template code that I am using. I am still getting that sort error.

  		<ul>
		  	<% control SortedChildren %>
		  	<% control Menu(2) %>
			<% if Children %><% else %><li><a href="$Link" title="Go to the $Title.XML page">$MenuTitle.XML</a></li><% end_if %>
			 <% if Children %> 
			 <% control Children %> 
			 <li><a href="$Link" title="Go to the $Title.XML page">$MenuTitle.XML</a></li>
			 <% end_control %> 
			 <% end_if %> 
  			<% end_control %>
  			<% end_control %>
  		</ul>

Avatar
dhensby

Community Member, 253 Posts

7 October 2010 at 10:24am

You are controlling in the wrong place as i see it.

Firstly, you need to decide if you want the Menu(2) to return an alphabetical list as well as the children of the items in the menu.

If you do, you need to overload the 'getMenu' function in the page_controller so that it returns alphabetised children.

In the template you then need to control SortedChildren *inside* the menu control (not outside). You will also have to move the sorted children to the model (page class) rather than the controller (page_controller class).

template to look like:

<ul>
<% control Menu(2) %>
<% if Children %>
<% control SortedChildren %>
...
<% end_control %>
<% else %> ... <% end_if %>
<% end_control %>

Your getMenu function should practically copy the one in SiteTree_Controller, except for the bit where it fetches the pages as this should sort by Title (or MenuTitle).

Good luck

Avatar
Haddog

Community Member, 10 Posts

5 January 2015 at 3:10pm

Hi there i was just looking at this and was wondering if anyone knows of a way to return a list of all children pages on the CMS so then you can choose which ones can have a link in the content area of the page.

was thinking of maybe using a checkbox to tick which ones displayed

Go to Top