select previous item in twig for loop
for-loop, twig
Solution
There's no built-in way to do that, but here's a workaround:
{% set previous = false %}
{% for item in data %}
Value : {{ item }}
{% if previous %}
{# use it #}
{% endif %}
{% set previous = item %}
{% endfor %}
The if is neccessary for the first iteration.
Problem
I use twig and have some data in array. I use for loop to access all data like this: ``` {% for item in data %} Value : {{ item }} {% endfor %} ``` Is it possible to access previous item in loop? For example: when I'm on n item, I want to have access to n-1 item.