<p>Untested, but this should work.</p>

<p>You can get the number of matrix blocks in the matrix field by using the Twig length filter like this (<a href="http://buildwithcraft.com/docs/matrix-fields" rel="nofollow">see Matrix documentation</a> - scroll to the end):</p>

<pre><code>{% set numberOfBlocks = entry.gallery | length %}
</code></pre>

<p>You should then be able to get the last block by directly referencing it like this (since the array of matrix blocks is zero-based):</p>

<pre><code>{% set lastBlock = entry.gallery[numberOfBlocks - 1] %}
</code></pre>

<p>Now you have the last block, accessing the image field should work as you had in your original code:</p>

<pre><code>{% set image = lastBlock.image.first() %}
&lt;img class="lazy" src="{{ siteUrl }}img/assets/loader.gif" data-src="{{ image.getUrl('galleryThumb') }}" data-src-retina="{{ image.getUrl('galleryThumbRetina') }}" alt="{{ image.title }}"&gt;
</code></pre>

CC BY-SA 3.0
2015-07-02T10:33:42.067Z
Excellent work Steve! I am exceptionally pleased with this answer due to the removal of the for loop, much cleaner.
Graham Winston
CC BY-SA 3.0
2015-07-02T15:08:26.743Z
To be fair, Alec's answer is better than mine. I had forgotten about Twig's `last` filter!
Steve Rowling
CC BY-SA 3.0
2015-07-02T09:55:08.443Z
2015-07-02T09:55:08.443Z
Steve Rowling
<p>Twig comes with a <code>last</code> filter which you should be able to use, so with your <code>entry.gallery</code>  you can do:</p>

<pre><code>{% set lastBlock = entry.gallery|last %}
</code></pre>

<p>Which will return the last element in the sequence, there's more info in the twig docs:</p>

<blockquote>
  <p>The last filter returns the last "element" of a sequence, a mapping, or a string:</p>
</blockquote>

<p><a href="http://twig.sensiolabs.org/doc/filters/last.html" rel="noreferrer">Twig last filter</a></p>

CC BY-SA 3.0
2015-07-02T10:10:57.807Z
2015-07-02T10:10:57.807Z
Alec Ritson
<p>I am pretty new to craft (just came from EE) and trying to output the last entry from a matrix field in my template: </p>

<pre><code>{% for block in entry.gallery.last() %}
    {% set image = block.image.first() %}
    &lt;img class="lazy" src="{{ siteUrl }}img/assets/loader.gif" data-src="{{ image.getUrl('galleryThumb') }}" data-src-retina="{{ image.getUrl('galleryThumbRetina') }}" alt="{{ image.title }}"&gt;
{% endfor %}
</code></pre>

<p>So this isn't working as I thought, i've done some snooping but have had no luck, any ideas?</p>

<p>Thanks
-Graham</p>

<templating><matrix>
How do I output the last matrix block?
# How do I output the last matrix block?

(Asked by: username_0 on 2015-07-02)

I am pretty new to craft (just came from EE) and trying to output the last entry from a matrix field in my template: 
{% for block in entry.gallery.last() %}
    {% set image = block.image.first() %}
    
{% endfor %}

So this isn't working as I thought, i've done some snooping but have had no luck, any ideas?
Thanks
-Graham

**Tags:** templating, matrix

---

## 2 answers

### Answer by username_1 on 2015-07-02. Score: 2 | Accepted answer

Untested, but this should work.
You can get the number of matrix blocks in the matrix field by using the Twig length filter like this (see Matrix documentation - scroll to the end):
{% set numberOfBlocks = entry.gallery | length %}

You should then be able to get the last block by directly referencing it like this (since the array of matrix blocks is zero-based):
{% set lastBlock = entry.gallery[numberOfBlocks - 1] %}

Now you have the last block, accessing the image field should work as you had in your original code:
{% set image = lastBlock.image.first() %}

> **Comments:**
> **username_0**: Excellent work Steve! I am exceptionally pleased with this answer due to the removal of the for loop, much cleaner.  
> **username_1**: To be fair, Alec's answer is better than mine. I had forgotten about Twig's `last` filter!  

---

### Answer by username_2 on 2015-07-02. Score: 5

Twig comes with a last filter which you should be able to use, so with your entry.gallery  you can do:
{% set lastBlock = entry.gallery|last %}

Which will return the last element in the sequence, there's more info in the twig docs:

The last filter returns the last "element" of a sequence, a mapping, or a string:

Twig last filter


---

