Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Returning Multiple ChildrenOf in a list

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: 453 Views
  • edwardlewis
    Avatar
    Community Member
    32 Posts

    Returning Multiple ChildrenOf in a list Link to this post

    Just got a quick question.. I'm trying to create a big ul of children of several different pages for a directory site I'm creating..

    At the moment in the page template I have got a few <% ChildrenOf (page-name)%> tags all listed out. Is there a better way to do it? I have been trying to create a function to group them all together but I cant work it out, something like this:

    function SpecificChildren() {

    static $allowed_children = array('page-name1', 'page-name2');

    }

    Should that work? Then in the template have something like this:

    <ul>
    <% control SpecificChildren %>
    <li><a href="$Link" title="$Title">$Title</a></li>
    <% end_control %>
    </ul>

    Is there a better way to do it?

  • Devlin
    Avatar
    Community Member
    186 Posts

    Re: Returning Multiple ChildrenOf in a list Link to this post

    Basically it is:

    <% if Children %>
    <ul>
    <% control Children %>
    <li><a href="$Link" title="$Title.XML">$Title.XML</a></li>
    <% end_control %>
    </ul>
    <% end_if %>

    If you want to blacklist/whitelist the display of the children, you could do something like this:

    class Page extends SiteTree {
       static $db = array("VisibleInList"=>"Boolean");
       function getCMSFields() {
          $fields = parent::getCMSFields();
          // @todo add checkbox for VisibleInList
          return $fields;
       }
    }

    <% if Children %>
    <ul>
    <% control Children %>
    <% if VisibleInList %>
    <li><a href="$Link" title="$Title.XML">$Title.XML</a></li>
    <% end_if %>
    <% end_control %>
    </ul>
    <% end_if %>

    Just an idea.

  • MonkeyBen
    Avatar
    Community Member
    25 Posts

    Re: Returning Multiple ChildrenOf in a list Link to this post

    You can do something like this: -

    In Template: -

    <% control FilteredChildren %>
    <% end_control %>

    In Page Controller: -

    public function getFilteredChildren()
    {
       $children= DataObject::get("SiteTree", "ClassName = 'HomePage' OR ClassName ='OtherTypePage'", "ClassName ASC");
       return $children? $children : false;
    }

  • MonkeyBen
    Avatar
    Community Member
    25 Posts

    Re: Returning Multiple ChildrenOf in a list Link to this post

    For those stumbling on this...you should use ID instead of ClassName...I knew what Ed wanted to do, which isn't mentioned on this thread :O}

    453 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.