Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Limiting <% control Children %>

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

Page: 1
Go to End
Author Topic: 6126 Views
  • imagic
    Avatar
    Community Member
    12 Posts

    Limiting <% control Children %> Link to this post

    Hi guys, I need some help with something.

    I have a nested children control, and simply want to limit the amount of results it displays..

    Could someone give me a hand please?

    Here's my code

                <% control ChildrenOf(articles) %>
                <div class="cat">
                <div class="header">$Title </div>
                 <ul>
                   <% control Children %>
        <li>
                       <a href="$Link">$Title</a>
                       <p>$Content.LimitWordCount(18)</p>
                      </li>   
                   <% end_control %>
                   </ul><br />
                   <a style="font-size:11px; font-weight:bold" href="$Link">More $Title articles...</a>
                </div>   
                <% end_control %>
                

    Cheers

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Limiting <% control Children %> Link to this post

    hi imagic

    I think you will need to create a custom function for this. There are two ways to do depending on wheher the child pages are of multiple types.

    These functions needs to go into your Article Page model (or whichever pagetype you want the children of) so that it can be called from within a <% control %> loop.

    This one will cover any child type

    public function GetChildren($Limit = 5){

    $Children = $this->Children();

    return $Children->getRange(0, $Limit);

    }

    or this one which will only return apges of a certain type

    public function GetChildren($Limit = 5){

    return DataObject::get("PageType", "ParentID = $this->ID", Null, Null, $Limit);

    }

    Hope that helps

    Aram

  • imagic
    Avatar
    Community Member
    12 Posts

    Re: Limiting <% control Children %> Link to this post

    Perfect!

    Thanks so much.

    I had a similar function made, but when I called it nothing happened, because I had the function in the wrong PHP file.

    Cheers for the info!

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Limiting <% control Children %> Link to this post

    Hi there

    Thank you Aram for such an excellent solution to this. I've got a very similar problem that hopefully someone can help me with.

    Some of our pages have nesting <% control Children %> loops so the Children and Grandchildren of a page is displayed:

    <% control Children %>
    Child Page: $Title
    <% control Children %>
    Grandchild Page: $Title
    <% end_control %>
    <% end_control %>

    We would like to limit the number of Grandchildren displayed but the following code using Aram's solution doesn't work:

    <% control Children %>
    Child Page: $Title
    <% control GetChildren(3) %>
    Grandchild Page: $Title
    <% end_control %>
    <% end_control %>

    If anyone could give me any ideas on how to limit the Grandchildren that'd be great (I'm hoping it's just a tweak on Aram's solution.)

    Cheers
    Tama

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Limiting <% control Children %> Link to this post

    Any ideas? I suppose I could write some custom functions but if there's a simpler solution I'd love to see it.

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Limiting <% control Children %> Link to this post

    My mistake was putting GetChildren inside "class Page_Controller extends ContentController {" in Page.php

    Putting it inside "class Page_Controller extends ContentController {" in Page.php and it works.

    So technically this is using the DataObject rather than the Controller.

  • wilsonStaff
    Avatar
    Community Member
    142 Posts

    Re: Limiting <% control Children %> Link to this post

    Hi, in the same spririt. Using your formula,

    in which controller do i insert the public function GetChildren

    in Page.php ? => where the call for the children is sent
    OR
    in PageActualiteToutes ? => whose children resides

    :: Page.ss

    ...
    <% loop ChildrenOf(actualites) %>
    <% control GetChildren(3) %>
          <article>
             <a href="$Link" title="$TitreActualite">
             $PhotoActualite.SetWidth(115)
             <p>$TexteActualite.LimitWordCount(10)<span> &raquo; </span></p>
             </a>
          </article>
    <% end_control %>
    <% end_loop %>
    ...

    Thanks!

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