Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-02T10:33:42.067Z
Answers.Text: Excellent work Steve! I am exceptionally pleased with this answer due to the removal of the for loop, much cleaner.
Answers.UserDisplayName: Graham Winston
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-02T15:08:26.743Z
Answers.Text: To be fair, Alec's answer is better than mine. I had forgotten about Twig's `last` filter!
Answers.UserDisplayName: Steve Rowling
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-02T09:55:08.443Z
Answers.LastActivityDate: 2015-07-02T09:55:08.443Z
Answers.UserDisplayName: Steve Rowling
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-02T10:10:57.807Z
Answers.LastActivityDate: 2015-07-02T10:10:57.807Z
Answers.UserDisplayName: Alec Ritson
Body: <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>
  
Tags: <templating><matrix>
Title: How do I output the last matrix block?
ThreadText: # 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() %}
      <img class="lazy" src="{{ siteUrl }}img/assets/loader.gif" data-src="{{ image.getUrl('galleryThumb') }}" data-src-retina="{{ image.getUrl('galleryThumbRetina') }}" alt="{{ image.title }}">
  {% endfor %}
  
  ```
  
  So this isn't working as I thought, i've done some snooping but have had no luck, any ideas?
  
  Thanks
  -username_0
  
  **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](http://buildwithcraft.com/docs/matrix-fields) - 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() %}
  <img class="lazy" src="{{ siteUrl }}img/assets/loader.gif" data-src="{{ image.getUrl('galleryThumb') }}" data-src-retina="{{ image.getUrl('galleryThumbRetina') }}" alt="{{ image.title }}">
  
  ```
  
  > **Comments:**
  > **username_0**: Excellent work username_1! I am exceptionally pleased with this answer due to the removal of the for loop, much cleaner.  
  > **username_1**: To be fair, username_2'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](http://twig.sensiolabs.org/doc/filters/last.html)
  
  
  ---
  
  