Include a loop within a loop does not work

SS4

Why don’t include files ,with a loop in it, work within a loop. See example below

//This does not work
<% loop $get_log_days %>
  <% include blood_values_List %>
<% end_loop %>

//This does work
<% loop $get_log_days %>
   <% loop $blood_values %>
      <div class="viewable_record">
         <ul>
             <li>mmol: $mmol</li>
         </ul>
      </div>
   <% end_loop %>
<% end_loop %>

I believe you will need to pass blood_values as a parameter in the include line

1 Like

Indeed, see the last snippet of this section: Template Syntax – SilverStripe Documentation

so something like

<% include blood_values_List blood_values=$blood_values %>

Should do it

2 Likes

In this example, what is blood_values_List? Includes are SilverStripe templates. I’ve just tested on my own SS4 site and can successfully achieve an Include inside a loop.

What is the desired result here? What is the content of your Include?

Hi Andante,

Thank you for your response.
‘blood_values_List’ is a include file. the inside of the file is the same as follows.


<% loop $blood_values %>
      <div class="viewable_record">
         <ul>
             <li>mmol: $mmol</li>
         </ul>
      </div>
<% end_loop %>

So it is not a problem to load static content but to loop over a has_many relation.
The reason why i want to do this is to keep my code as modular as possible. This way i could for example reuse this include template for returning an Ajax post.

Thank you very much, that worked indeed.

Please mark an answer as accepted :slight_smile:

1 Like