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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

List of child pages in alphabetical order


Reply


7 Posts   2376 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, 1799 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, 1799 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
Pigeon

Community Member, 243 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