The count twig extension gives you a simple and efficient way to have a global counter in your twig template.
The twig extension need to be registered as symfony service.
services:
Sulu\Twig\Extensions\CountExtension: ~
If autoconfigure is not active you need to tag it with twig.extension.
You can increase and get the current counter with:
{{ counter('example') }}
{{ counter('example') }}
{{ counter('test') }}
{{ counter('test') }}
{% do reset_counter('example') %}
{{ counter('example') }}
Output:
1
2
1
2
1
A more real use case would be to check for odd or even:
<div class="section{% if counter('section') is odd %} section--black{% endif %}">