How can I determine if the row being returned from a table field in a for loop is the last row?
2 Answers
Twig's for
loop exposes a loop
variable with a number of useful properties, including last
:
{% for row in table %}
...
{% if loop.last %}
// Do stuff
{% endif %}
{% endfor %}
See the official Twig docs for more information on the loop
variable and its properties (first, last, index etc).
In any for loop you can use the loop
variable that twig gives you.
For example:
{% for row in myTable %}
{% if loop.last %}
This is the last row
{% else %}
This is anything but the last row
{% endif %}
{% endfor %}
More information on the loop variable can be found in the twig docs.