twig for key value code example

Example 1: foreach twig

{% for user in users %}
        <li>{{ user.username|e }}</li>
    {% endfor %}

Example 2: twig for

<ul>
    {% for key, user in users %}
        <li>{{ key }}: {{ user.username|e }}</li>
    {% endfor %}
</ul>

Example 3: twig loop variables

{% for user in users %}
    {{ loop.index }} - {{ user.username }}
{% endfor %}

Example 4: twig for loop key

{% for key, user in users %}
	<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}

Example 5: twig foreach key value

{% for key, user in users %}
    <li>{{ key }}: {{ user.username }}</li>
{% endfor %}