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

Limiting <% control Children %>


Go to End


7 Posts   11935 Views

Avatar
imagic

Community Member, 12 Posts

6 October 2009 at 11:55am

Edited: 06/10/2009 11:56am

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

Avatar
Carbon Crayon

Community Member, 598 Posts

6 October 2009 at 1:52pm

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

Avatar
imagic

Community Member, 12 Posts

6 October 2009 at 2:06pm

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!

Avatar
Tama

Community Member, 138 Posts

25 June 2010 at 1:33pm

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

Avatar
Tama

Community Member, 138 Posts

22 July 2010 at 9:59am

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

Avatar
Tama

Community Member, 138 Posts

27 July 2010 at 10:34am

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.

Avatar
wilsonStaff

Community Member, 143 Posts

23 October 2013 at 2:15pm

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!