symfony - Twig variable from one loop in another doesn't work -
i have 2 loops in twig. 1 check stock level of product, other display product options. i'm trying create variable 1 loop can use inside other add class name list(s).
i can't work. more welcome...
what have this:
{% set stocklevel = '' %} {% variant in product.variants %} {% set stocklevel = variant.stock.level %} {{ stocklevel }} // gives 1 0 1 1 (so sizes except second 1 available) {% endfor %} {% option in product.options %} <ul class="optionslist"> {% if option.values %} {% value in option.values %} <li class=" {% if stocklevel == 0 %}not_on_stock {% endif %}" > <a class="item">etc...</a> </li> {% endfor %} {% endif %} </ul> {% endfor %}
i believe there wrong way handling data. right now, event if variable accessible in second for loop, value of 1 set lastly , script would've failed anyway.
i can suggest bit of hacky way, feel free improvise or use idea behind it.
say, assign empty array:
{% set variantsarray = [] %}
then, use filter merge fill dummy data(that's first loop comes play)
{% number in 1..5 %} {% set variantsarray = variantsarray | merge([number]) %} {% endfor %}
then sure, can access values there, dump out(that's second loop comes)
{% index in 1..3 %} {{ dump(variantsarray) }} {% endfor %}
the following dump generated:
array:5 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 ]
hope got idea. suggestions kindly welcome.