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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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

Returning Multiple ChildrenOf in a list


Go to End


4 Posts   996 Views

Avatar
edwardlewis

Community Member, 33 Posts

23 February 2012 at 2:16pm

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

Community Member, 344 Posts

23 February 2012 at 11:50pm

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

Community Member, 25 Posts

24 February 2012 at 4:26am

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

Community Member, 25 Posts

25 February 2012 at 9:21am

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}