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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Looping in loop


Reply

7 Posts   416 Views

Avatar
orkhan

28 February 2012 at 8:28am Community Member, 4 Posts

<% control AllChildren %>
<div class="map_item">
<h1 class="$Color">$Title</h1><div class="image">$Photo</div>
<ul>
<% control MyLoopFunction(2) %>
<li><a href="$Link">$Title</a></li>
<% end_control %>
</ul>
</div>
<% end_control %>

In this example MyLoopFunction is not working. But MyLoopFunction working without looping AllChildren
<ul>
<% control MyLoopFunction(2) %>
<li><a href="$Link">$Title</a></li>
<% end_control %>
</ul>

Please help me anyone

sorry for my English

Avatar
martimiz

28 February 2012 at 10:24pm Forum Moderator, 1091 Posts

Hi orkhan, welcome to the forums

I assume MyLoopFunction() is a function in your Page_Controller? That will not work within the <% control AllChildren %> loop because it returns a set of Page objects (the data model class), not Page_Controllers. So in order for this to work, you'd have to define the MyLoopFunction() funtion in your Page class instead.

Avatar
orkhan

28 February 2012 at 10:47pm Community Member, 4 Posts

<?php

class Applications extends SiteTree {

   ....

}

class Applications_Controller extends ContentController {

   .....

   function MyLoopFunction($id=1) {
$children = DataObject::get("SolutionsPage", '`SolutionsPage_AppsTypes`.`AppsTypeID` = '.$id, "Created DESC", "LEFT JOIN `SolutionsPage_AppsTypes` ON `SolutionsPage_AppsTypes`.`SolutionsPageID` = `SolutionsPage`.`ID`", 100);

return $children;
}
}

This is my Applications class extends to SiteTree

What there is not true?

Avatar
swaiba

28 February 2012 at 11:09pm Forum Moderator, 1796 Posts

What there is not true?

As martimz says - context is wrong. Control is not just a "loop" but also changes "context" you start out int he Page_Controller object, then you move context into the Page object. This is changing to be more clear in SS3.

Just try moving your function from "Applications_Controller" to "Applications" and see what happens...

Avatar
orkhan

28 February 2012 at 11:30pm Community Member, 4 Posts

Thanks it is worked. But when i try change my code to this

<% control ChildsLoop(Pos) %>
result is
Sorry, there was a problem with handling your request.

how to fix it?

Avatar
swaiba

28 February 2012 at 11:45pm Forum Moderator, 1796 Posts

you can only pass fixed strings into methods on the template - whereas $Pos may provide a number when merged it cannot be used as a function paramter.

This comes up over and over, so I have a post to refernce for you now...

http://www.silverstripe.org/template-questions/show/13619#post304007#post304007

Avatar
orkhan

28 February 2012 at 11:57pm Community Member, 4 Posts

Thanks :) Please close topic :)