Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » ChildrenOf() Call

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2893 Views
  • brb5548
    Avatar
    Community Member
    17 Posts

    ChildrenOf() Call Link to this post

    Hi,
    I am trying to use the ChildrenOf function and everything works great as long as I hard code the page-url value. Does anyone know if I can pass a parameter that represents the url of the current page instead of hard coding this value?

    My goal is to create a menu bar that only shows the child pages and I am having a hard time.

    Sample Code:
    <% if Children %>
    <h1>Menu</h1>
    <div class="submenu">

    <% control ChildrenOf(photos) %>

    <a href="$Link" title="Go to the $Title.XML page" <% if LinkingMode %>class="$LinkingMode"<% end_if %>>$MenuTitle</a>   
             
    <% end_control %>
    </div>
    <% end_if %>

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: ChildrenOf() Call Link to this post

    Hey there,

    To be clear - you want to control the children of the current page?

    The standard: <% control Children %> should do the trick.

  • brb5548
    Avatar
    Community Member
    17 Posts

    Re: ChildrenOf() Call Link to this post

    There seems to be a problem with just using the Children call. For example, if I layout several pages in a 2 or 3 level format like:
    -Home
    +News
    +Calendar

    When you click on the Home page you can see the Children call display the sub-pages properly. (News and Calendar) But when you click on the News page you will still have the sub-pages displayed of the Home page. (News and Calendar)
    So it seems with 2nd and 3rd level pages, the Children call returns only the sub-links of the parent page. Here is the code that I am using during the above test.

    <% control Level(1) %>

    <% if Title = Blog %>
    <% else %>
    <% if Title = Forums %>
    <% else %>

    <% if Children %>
    <h1>Menu</h1>
    <div class="submenu">

    <% control Children %>
    <a href="$Link" title="Go to the $Title.XML page" <% if LinkingMode %>class="$LinkingMode"<% end_if %>>$MenuTitle</a>   
             
    <% end_control %>
    </div>

    <% end_if %>
    <% end_if %>
    <% end_if %>

    <% end_control %>

    $SideBar
    <% include Cart %>

    Any ideas??

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: ChildrenOf() Call Link to this post

    That makes perfectly sense...

    You're doing <% control Level(1) %>

    So if you're layout is like

    -Home
    ----News
    ----Calendar

    With this code you're using - it will always fetch the 'home' subpages... because you're in level1 all the time... I think...

  • brb5548
    Avatar
    Community Member
    17 Posts

    Re: ChildrenOf() Call Link to this post

    That was my first thought so during my testing, I had that control level(1) line removed (I had to actually add it back in to my previous post with an edit)
    What I found out is that you are correct and that control level needs to be removed and you have to nest the link iterations for each child control. The big thing that I was missing was a conditional check for LinkOrCurrent = current before each child control iteration.
    The code below works but I was hoping to find a cleaner way. If someone comes across this post and has a better way to display only sub-page links to the current page, please let everyone know.
    Thanks for the help.

    <% if Title = Blog %>
    <% else %>
    <% if Title = Forums %>
    <% else %>

    <% if LinkOrCurrent = current %>

    <% if Children %>
    <h1>Menu</h1>
    <div class="submenu">

    <% control Children %>
    <a href="$Link" title="Go to the $Title.XML page" <% if LinkingMode %>class="$LinkingMode"<% end_if %>>$MenuTitle</a>   

    <% if LinkOrCurrent = current %>
    <% if Children %>
    <ul>
    <% control Children %>
    <li><a href="$Link" title="Go to the $Title.XML page" <% if LinkingMode %>class="$LinkingMode"<% end_if %>>$MenuTitle</a></li>
    <% end_control %>
    </ul>
    <% end_if %>
    <% end_if %>
    <% end_control %>
    </div>

    <% end_if %>
    <% end_if %>
    <% end_if %>

    <% end_if %>

    $SideBar
    <% include Cart %>

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: ChildrenOf() Call Link to this post

    All I usually need to do to show the children is

    <% if Children %>
    <% control Children %>
       <a href="$Link" title="Go to the $MenuTitle.ATT page" class="$LinkingMode">$MenuTitle.XML</a>
    <% end_control %>
    <% end_if %>

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: ChildrenOf() Call Link to this post

    Hey brb - just looking at the last piece of code you posted:

    As far as I can see, the first <% if LinkOrCurrent = current %> should always evaluate to true, since you're always controlling the 'current' page by default (unless there are control statements elsewhere that we can't see).

    Your second <% if LinkOrCurrent = current %> should always evaluate to false, since it's within a control statement for the children pages of the 'current' page (and because you've already checked that another page was 'current' earlier, and now you're controlling a different page -- it has to be false!)

    That's just from eyeballing the code, but if you remove those pieces then you end up with exactly what dio posted.

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