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

Nested loop with parameter


Go to End


3 Posts   2973 Views

Avatar
PeterR

Community Member, 15 Posts

22 January 2015 at 8:48pm

Edited: 22/01/2015 11:36pm

Hi all,

I want to display nested loops.
My simple model is following. Category can contain tasks. I want to loop categories and show all tasks within category.

My controller is following:

    public function GetCategories() {        
        return Office_TaskCategory::get();
    }

    public function GetTasks($category) {        
        return Office_Task::get()->filter(array(
            'CategoryID' => intval($category)
        ));
    }

and view:
<% loop $GetCategories %>
   $Title 
         <ul>
               <% loop $GetTasks($ID) %>
                      <li>$Title</li>
                <% end_loop %>
           </ul>
 <% end_loop %>

but it returns only Title of categories, it not iterates through tasks.

Could you please help me how to fix it?

thanks a lot

Avatar
martimiz

Forum Moderator, 1391 Posts

23 January 2015 at 12:18am

Note that within the $GetCategories loop the scope changes to that of a Category DataObject. So your GetTasks() function needs to live within your Category class, not in any Controller...

Avatar
PeterR

Community Member, 15 Posts

23 January 2015 at 12:28am

Thanks I lot!

I put method into my DataObject class Office_TaskCategory and it works! :)