Hello i would like do somthing like that:
<?php $count = 0; foreach($a as $v): $count++; ?> <?php if ($count%2 == 0): ?> ... <?php endif; ?> <?php endforeach; ?>
in twig:
{% for v in a %} {% if ??? is even %} ... {% endif %} {% endfor %}
but how can i have a variable evolving with loop ?
The Counting Variable keeps track of the number of times the Repeat Dialog (or Loop) has been repeated. The number associated with a particular loop constitutes the value of the Counting Variable. That value is then appended to each variable name used in a Question which is part of that loop.
Use the enumerate() function to count in a for loop, e.g. for index, item in enumerate(my_list): . The function takes an iterable and returns an object containing tuples, where the first element is the index, and the second - the item.
The for loop is a count controlled loop. Count controlled loops must possess three elements: Initialization: it must initialize a counter variable to a starting value. Test: it must test the counter variable by comparing it to a final value.
Apparently twig defines some loop variables inside the for-loop:
{% for v in a %} {% if loop.index0 is even %} ... {% endif %} {% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With