Answers.Body: <p>Inside of a for-loop you have access to some <a href="https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable" rel="nofollow noreferrer">special variables</a>.</p>
  
  <pre><code>{% for block in entry.myMatrixField %}
      {% set isEndBlock = loop.revindex &lt;= 3 %}
  
      {% if not isEndBlock %}
          {# do things #}
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>Another approach would be to change / override the default criteria of the Matrix field’s ElementCriteriaModel.</p>
  
  <pre><code>{% set blocksCount = entry.myMatrixField.count() %}
  {% set isValid = blocksCount - 3 &gt; 0 %}
  
  {% set startBlocks = isValid ? entry.myMatrixField({
      limit: blocksCount - 3,
  }) : null %}
  </code></pre>
  
  <p>Or criteria to get the last 3 blocks:</p>
  
  <pre><code>{% set endBlocks = entry.myMatrixField({
      order: 'matrixblocks.sortOrder desc',
      limit: 3,
  }) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-22T10:27:02.537Z
Answers.LastActivityDate: 2017-11-22T11:03:12.420Z
Answers.LastEditDate: 2017-11-22T11:03:12.420Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>You can fetch all elements and slice the last 3 elements from them</p>
  
  <pre><code>{% set entriesList = entry.fieldHandle.find() %}
  {% set start = entriesList|length - 3 %}
  {% set end = entriesList|length %}
  {% set lastThree = entriesList | slice(start, end) %}
  </code></pre>
  
  <p>Of course you need to make sure you have at least 3 objects your array</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-22T10:00:10.847Z
Answers.LastActivityDate: 2017-11-22T10:00:10.847Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I would like to include a file on every block but the last three. How can I accomplish this?</p>
  
Tags: <templating><matrix>
Title: Get the three last Matrix blocks
ThreadText: # Get the three last Matrix blocks
  
  (Asked by: username_0 on 2017-11-22)
  
  I would like to include a file on every block but the last three. How can I accomplish this?
  
  **Tags:** templating, matrix
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-11-22. Score: 2 | Accepted answer
  
  Inside of a for-loop you have access to some [special variables](https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable).
  
  ```
  {% for block in entry.myMatrixField %}
      {% set isEndBlock = loop.revindex <= 3 %}
  
      {% if not isEndBlock %}
          {# do things #}
      {% endif %}
  {% endfor %}
  
  ```
  
  Another approach would be to change / override the default criteria of the Matrix field’s ElementCriteriaModel.
  
  ```
  {% set blocksCount = entry.myMatrixField.count() %}
  {% set isValid = blocksCount - 3 > 0 %}
  
  {% set startBlocks = isValid ? entry.myMatrixField({
      limit: blocksCount - 3,
  }) : null %}
  
  ```
  
  Or criteria to get the last 3 blocks:
  
  ```
  {% set endBlocks = entry.myMatrixField({
      order: 'matrixblocks.sortOrder desc',
      limit: 3,
  }) %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2017-11-22. Score: 2
  
  You can fetch all elements and slice the last 3 elements from them
  
  ```
  {% set entriesList = entry.fieldHandle.find() %}
  {% set start = entriesList|length - 3 %}
  {% set end = entriesList|length %}
  {% set lastThree = entriesList | slice(start, end) %}
  
  ```
  
  Of course you need to make sure you have at least 3 objects your array
  
  
  ---
  
  