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   486 Views

Avatar
orkhan

Community Member, 4 Posts

28 February 2012 at 8:28am

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

Forum Moderator, 1132 Posts

28 February 2012 at 10:24pm

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

Community Member, 4 Posts

28 February 2012 at 10:47pm

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

Forum Moderator, 1805 Posts

28 February 2012 at 11:09pm

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

Community Member, 4 Posts

28 February 2012 at 11:30pm

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

Forum Moderator, 1805 Posts

28 February 2012 at 11:45pm

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

Community Member, 4 Posts

28 February 2012 at 11:57pm

Thanks :) Please close topic :)