Hi there I am trying to nest a fori loop inside a for loop (2nd level) with independent variables using the following code:
{% for a in keys_array %}
{% fori item in custom[a.key] %}
|{% input item.description %}
{% endfori %}
{% endfor%}
What am I missing?
Thanks,
Borja
Hi @borjaGonzalez forloops donβt have keys, only forloops do.
If all your elements in the keys_array are unique, you can do the following:
{% for a in keys_array %}
{% assign a_key = a | replace:" ","_" %}
{% fori item in custom[a_key] %}
|{% input item.description %}
{% endfori %}
{% endfor%}
I added this line
{% assign a_key = a | replace:" β,β_" %}
So that the spaces are replaced by an underscore.
Alternatively you could also use
forloop.index or forloop.index0
This is however not recommended since adding an element to the array might mess things up.
Kind regards
Sam
1 Like
Hi Sam,
Many thanks for your answer. The values in keys_array are unique, however when applying the solution you gave, instead having the input boxes in the foriloop, the values in keys_array are printed (as per the image below).
Best,
Borja
Hi again @Sam,
I managed to find a solution. The problem was that my keys were arranged as follows:
{% capture keys_array %}section001|section001q001|section001q002!section002|section002q001|section002q002|section002q003!section004!{% endcapture %}
{% assign keys_array = keys_array | split:'!' %}
The pipe symbol β|β separating subsections was causing the issue so I substituted it for β?β and it works now.
Thanks,
Borja
@borjaGonzalez
{% capture keys_array %}section001|section001q001|section001q002!section002|section002q001|section002q002|section002q003!section004!{% endcapture %}
{% assign keys_array = keys_array | split:'!' %}
The problem actually was that the symbol you were using to split. You used ! while it should have been |. The symbols used to split most of the time are | and ; by the way.
Kind regards
Sam
1 Like