Reputation: 7739
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 ?
Upvotes: 34
Views: 49799
Reputation: 12537
Apparently twig defines some loop variables inside the for-loop:
{% for v in a %}
{% if loop.index0 is even %}
...
{% endif %}
{% endfor %}
Upvotes: 66
Reputation: 747
If you use it for styling you can do:
{% for v in a %}
<div class="link {{ cycle(['even', 'odd'], loop.index0) }}">
</div>
{% endfor %}
Upvotes: 24