Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A count in a loop

Tags:

twig

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 ?

like image 452
bux Avatar asked Dec 26 '11 17:12

bux


People also ask

What is a count variable 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.

How do you do a count loop in Python?

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.

Is for loop is a counting loop?

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.


1 Answers

Apparently twig defines some loop variables inside the for-loop:

{% for v in a %}     {% if loop.index0 is even %}         ...     {% endif %} {% endfor %} 
like image 91
vstm Avatar answered Oct 01 '22 15:10

vstm