Jump to:

23360 Posts in 18124 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » List of child pages in alphabetical order

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

Page: 1
Go to End
Author Topic: 2195 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    List of child pages in alphabetical order Link to this post

    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>

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: List of child pages in alphabetical order Link to this post

    create a method SortedChildren in your controller and...

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

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: List of child pages in alphabetical order Link to this post

    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>

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: List of child pages in alphabetical order Link to this post

    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?

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: List of child pages in alphabetical order Link to this post

    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;
        }
       }

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: List of child pages in alphabetical order Link to this post

    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>

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: List of child pages in alphabetical order Link to this post

    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

    2195 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.