Jump to:

23002 Posts in 11855 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Looping in loop

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

Page: 1
Go to End
Author Topic: 350 Views
  • orkhan
    Avatar
    Community Member
    4 Posts

    Looping in loop Link to this post

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

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Looping in loop Link to this post

    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.

  • orkhan
    Avatar
    Community Member
    4 Posts

    Re: Looping in loop Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Looping in loop Link to this post

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

  • orkhan
    Avatar
    Community Member
    4 Posts

    Re: Looping in loop Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Looping in loop Link to this post

    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

  • orkhan
    Avatar
    Community Member
    4 Posts

    Re: Looping in loop Link to this post

    Thanks Please close topic

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