Jump to:

3392 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Can someone explain Children() and ChildrenOf() controls please?

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

Page: 1 2
Go to End
Author Topic: 7886 Views
  • thepurpleblob
    Avatar
    Community Member
    28 Posts

    Can someone explain Children() and ChildrenOf() controls please? Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Can someone explain Children() and ChildrenOf() controls please? Link to this post

    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.

  • SSadmin
    Avatar
    Community Member
    90 Posts

    Re: Can someone explain Children() and ChildrenOf() controls please? Link to this post

    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?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Can someone explain Children() and ChildrenOf() controls please? Link to this post

    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

  • SSadmin
    Avatar
    Community Member
    90 Posts

    Re: Can someone explain Children() and ChildrenOf() controls please? Link to this post

    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

  • mmilo
    Avatar
    Community Member
    9 Posts

    Re: Can someone explain Children() and ChildrenOf() controls please? Link to this post

    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?

  • lerni
    Avatar
    Community Member
    80 Posts

    Re: Can someone explain Children() and ChildrenOf() controls please? Link to this post

    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 %>

  • mmilo
    Avatar
    Community Member
    9 Posts

    Re: Can someone explain Children() and ChildrenOf() controls please? Link to this post

    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.

    7886 Views
Page: 1 2
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.