Question:
Loop issue with retrieving parent values inside child loop
Details of your query go here
I am facing a strange issue in Silverstripe version 4.x. In nested loop, child loop does not picking the value of parent loop.
`$get_parent = ['id'=>1,'name'=>test];
$get_child = ['class'=>'btech,'subject'=>'test'];`
`<% loop $get_parent %>
<% loop $get_child %>
// i want get value of parent loop
<% end_loop %>
<% end_loop %>`
What you are seeing sounds normal. Once you are in the child loop, you are within its scope so the parent data can’t be seen.
You should be able to access the parent data with the $Up method in your template, eg:
<% loop $get_parent %>
<% loop $get_child %>
$class <!-- the 'class' value of the current item in the child loop -->
$Up.name <!-- should provide the name of the current item in the parent loop -->
<% end_loop %>
<% end_loop %>
It seems like you’re getting some confusion over the scopes… in your example, when you do <% loop $Up.get_child %> then what you are seeing is expected… you won’t get any value for $name because you have changed scope again.
Could you post a bit more of your code? and maybe tell us what the final result is that you want to achieve? It will help us to provide some more meaningful advice.