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

Returning Multiple ChildrenOf in a list


Reply

4 Posts   619 Views

Avatar
edwardlewis

23 February 2012 at 2:16pm Community Member, 32 Posts

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?

Avatar
Devlin

23 February 2012 at 11:50pm Community Member, 215 Posts

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.

Avatar
MonkeyBen

24 February 2012 at 4:26am Community Member, 25 Posts

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

Avatar
MonkeyBen

25 February 2012 at 9:21am Community Member, 25 Posts

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}