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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Can someone explain Children() and ChildrenOf() controls please?


Go to End


9 Posts   15704 Views

Avatar
thepurpleblob

Community Member, 28 Posts

21 September 2009 at 9:46am

I'm actually trying to develop a css based two-level drop down menu. So I actually need the submenu generation to be nested inside the Menu(1) control.

I've read the documentation but it makes, pretty much, no sense. The comment by <% control Children %> seems tempting, but doesn't give enough information to work out what it actually does. An example would help too! ChildrenOf is listed right next to the menu items so would seem related but needs a URL as a parameter (or what and why?)

I'm trying to think of a more constructive question but I can't figure out what I want to do because I don't understand much of the documentation. Any help appreciated!

Avatar
Willr

Forum Moderator, 5523 Posts

21 September 2009 at 10:19am

Children returns the child objects of the current page scope. For example if you are on a blog page - <% control Children %> returns the list of children under this blog page - eg the entries in the blog.

// on the blog page
<% control Children %>
// the children of blog
<% end_control %>

<% control Children %> can be nested so you can control the children in any other scope. Eg for menus etc

// on blog page
<% control Menu(1) %>
// the current 'scope' is now on the individual menu pages (eg 'Home', 'About', 'Blog'). Not the blog
<% control Children %>
// returns the children of home, then children of about, then children of blog
<% end_control %>
<% end_control %>

<% control ChildrenOf(my-page-url) %> changes the scope to a page with the given url. Returns the children of the page with the url segment 'my-page-url'

// on the Home Page
<% control ChildrenOf(blog) %>
// now its like calling Children() on the blog page
<% end_control %>

Hope that clears it up.

Avatar
SSadmin

Community Member, 90 Posts

9 February 2010 at 2:53pm

i have tried the<% Control ChildrenOf(url) %> stuff.. but not sure how it works..

site tree

-home url(www.xxxx.com/home)
+about us url(www.xxx.com/about-us)
-location url(www.xxx.com/location)
-staff url(www.xxx/staff)
+Listings url(www.xxx/listings)
-Listing1
-Listing2

In my navigation menu, i just want to show the Home, About-us (children pages), Lisings(No children pages)

So im doing:

<div id="nav-wrap">
<div id="nav" class="clearfix container">
<ul>
<% control Menu(1) %>
<li>
<a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle.XML</a>

<ul class="transparent clearfix">

<% control ChildrenOf(about-us) %>
<li>
<a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle.XML</a>
</li>
<% end_control %>

</ul>

</li>
<% end_control %>
</ul>
</div> <!-- end div id nav -->
</div><!-- end div id nav-wrap -->

It seems doesnt work..
Is there something wrong with url of <% control ChildrenOf(url) %> in this case?

Avatar
Sean

Forum Moderator, 922 Posts

9 February 2010 at 3:11pm

Edited: 09/02/2010 3:12pm

I don't think you can use that while inside a Menu(1) control. If it worked, then you'd get the children of about-us multipled by the amount of items in the menu. ChildrenOf will only work when used outside of the Menu control.

The idea is ChildrenOf is used for things like footer navigation, so you'd have a page called say "Footer Pages" which acts as a placeholder in the CMS. From here, you can then use <% control ChildrenOf(footer-pages) %> in your template to get all the footer pages.

For listing nested pages, you'll want to use the Children control. For example:


<ul id="menu1">
<% control Menu(1) %>
	<li>
		<a href="$Link">$MenuTitle</a>
	<% if Children %>
		<ul id="menu2">
		<% control Children %>
			<li><a href="$Link">$MenuTitle</a></li>
		<% end_control %>
		</ul>
	<% end_if %>
	</li>
<% end_control %>
</ul>

And another example for use of ChildrenOf...


<% if ChildrenOf(footer-pages) %>
<ul id="footer-menu">
	<% control ChildrenOf(footer-pages) %>
		<li><a href="$Link">$MenuTitle</a></li>
	<% end_control %>
</ul>
<% end_if %>

Cheers,
Sean

Avatar
SSadmin

Community Member, 90 Posts

9 February 2010 at 4:32pm

Hey,thx sean.

Really helpful suggestions.

i will keep in mind not using the Control childrenOf while in Menu Level control.

Your example is quite helpful for clearing up my concerns.XD

Avatar
mmilo

Community Member, 9 Posts

11 February 2010 at 7:42pm

Just wondering if it's possible to sort and filter these datafeeds. Say for instance I have a news section with articles under it. But then I want to grab the single most recent news article to display the home page.

Grabbing the articles using ChildrenOf(news) is easy enough, but that gives me all of them instead of just the most recent one. How does one filter this data?

Avatar
lerni

Community Member, 81 Posts

11 February 2010 at 9:07pm

heho

make a separate getter in php with get_one and then control this one in the template
http://doc.silverstripe.org/doku.php?id=datamodel

... or make a nested control in the themplate like:
<% ChildrenOf(news) %>
<% if First %>
...
<% end_if %>
<% end_control %>

Avatar
mmilo

Community Member, 9 Posts

12 February 2010 at 12:14am

Awesome thanks,

if First seems to work, but I'm guessing it's grabbing the first item in the order it's displayed in the CMS, which is fine, but it means you need to ensure that the latest news item is always on top, since SilverStripe's default seems to be to place it at the bottom.

I really like the idea of creating your own getter for achieving this task, as I assume you would be able to specify things like date of creation. Would you care to elaborate on how the syntax for something like this would would as the documentation only has

$record = DataObject::get_one($obj, $filter);

which doesn't really make it particularly clear where this code needs to be or what objects and filters it needs.

Go to Top