<p>Twig's <code>for</code> loop exposes a <code>loop</code> variable with a number of useful properties, including <code>last</code>:</p>

<pre><code>{% for row in table %}
    ...
    {% if loop.last %}
        // Do stuff
    {% endif %}
{% endfor %}
</code></pre>

<p>See <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable" rel="nofollow">the official Twig docs</a> for more information on the <code>loop</code> variable and its properties (first, last, index etc).</p>

CC BY-SA 3.0
2015-04-15T11:31:20.323Z
2015-04-15T11:31:20.323Z
Mats Mikkel Rummelhoff
<p>In any for loop you can use the <code>loop</code> variable that twig gives you.</p>

<p>For example:</p>

<pre><code>{% for row in myTable %}
  {% if loop.last %}
    This is the last row
  {% else %}
    This is anything but the last row
  {% endif %}
{% endfor %}
</code></pre>

<p>More information on the loop variable can be found in the <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable" rel="nofollow">twig docs</a>.</p>

CC BY-SA 3.0
2015-04-15T11:30:32.130Z
2015-04-15T11:30:32.130Z
Josh Angell
<p>How can I determine if the row being returned from a table field in a for loop is the last row?</p>

<templating><forloop><table-field>
Get last row in table field for loop
# Get last row in table field for loop

(Asked by: username_0 on 2015-04-15)

How can I determine if the row being returned from a table field in a for loop is the last row?

**Tags:** templating, forloop, table-field


---

## 2 answers

### Answer by username_1 on 2015-04-15. Score: 2 | Accepted answer

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).


---

### Answer by username_2 on 2015-04-15. Score: 3

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.


---

