<blockquote>
  <p>Additionally, it appears I cannot use the loop to save a variable to use outside of it</p>
</blockquote>

<p>This is really a separate question. Answering it anyway: you need to give the variable some value before the loop, and then it works:</p>

<pre><code>{% set img = null %} {# initial value #}

{% for block in entry.pageContent %}
  {% if block.type == 'images' %}
    {% if not img %}
      {% set img = block.images[0] ?? null %}
    {% endif %}
  {% endif %}
{% endfor %}

{# img will be the first image of first 'images' block, or null #}
</code></pre>

CC BY-SA 3.0
2018-03-18T21:29:30.313Z
2018-03-18T21:29:30.313Z
Marion Newlevant
<p>This article may be helpful to you: <a href="https://nystudio107.com/blog/speed-up-your-craft-cms-templates-with-eager-loading" rel="nofollow noreferrer">Speed up your Craft CMS Templates with Eager Loading</a></p>

<p>Basically, when you eager load the Matrix blocks, you're no longer getting an ElementCriteriaModel, rather you're getting an array pre-loaded with the Matrix blocks. That's why you can't do something like <code>entry.pageContent.type('images').first.images</code></p>

CC BY-SA 3.0
2017-10-19T17:01:18.583Z
2017-10-19T17:01:18.583Z
andrew.welch
<p>I would like to eager load: </p>

<ol>
<li>A list of entries</li>
<li>Within the entries loop, the first image, from the first of a block type on a matrix field.</li>
</ol>

<p><strong>Loading the entries like this</strong></p>

<pre><code>{% set entries = craft.entries({
  sectionId: 12,
  with: [
    'pageContent.text:text',
    ['pageContent.images:images', {
      withTransforms: [{
        width: 100,
        height: 100
      }]
    }]
  ]
}) %}
</code></pre>

<p><strong>Loading the images:</strong></p>

<p>Currently, I can only seem to load an image like this:</p>

<pre><code>{% for block in entry.pageContent %}
  {% if block.type == 'images' %}
    {% if img is not defined %}
      {% set img = block.images[0] ?? null %}

  {% endif %}
{% endfor %}```
</code></pre>

<p>I cannot seems to filter the query to the matrix down like you can when getting a matrix normally, I just receive a bunch of errors, I am trying  something like:</p>

<pre><code>{% set img = entry.content.type('images').first.images[0] ?? null %}
</code></pre>

<p>Additionally, it appears I cannot use the loop to save a variable to use outside of it, I presume this is intentional and how eager loading works. If that is the case, would I even benefit from any optimisation on the query at all if it is looping on blocks I do not need? As an example, I would normally do something like</p>

<pre><code>{% set img = entry.content.type('images').first.images.first ?? null %}
</code></pre>

<p>Following on from whether this is worth it... I also want to get a text field on this matrix (from the first of a block type again), but, in another position in the template. Does this mean I need to do another loop again to get the data specifically for another field?</p>

<p>Is there a better way to deal with this?</p>

<p>I hope that makes sense, let me know if not I'll do my best to clarify.</p>

<eager-loading>
Eager load a list of entries, then only the first asset, from the first block type
# Eager load a list of entries, then only the first asset, from the first block type

(Asked by: username_0 on 2017-10-19)

I would like to eager load: 

A list of entries
Within the entries loop, the first image, from the first of a block type on a matrix field.

Loading the entries like this
{% set entries = craft.entries({
  sectionId: 12,
  with: [
    'pageContent.text:text',
    ['pageContent.images:images', {
      withTransforms: [{
        width: 100,
        height: 100
      }]
    }]
  ]
}) %}

Loading the images:
Currently, I can only seem to load an image like this:
{% for block in entry.pageContent %}
  {% if block.type == 'images' %}
    {% if img is not defined %}
      {% set img = block.images[0] ?? null %}

  {% endif %}
{% endfor %}```

I cannot seems to filter the query to the matrix down like you can when getting a matrix normally, I just receive a bunch of errors, I am trying  something like:
{% set img = entry.content.type('images').first.images[0] ?? null %}

Additionally, it appears I cannot use the loop to save a variable to use outside of it, I presume this is intentional and how eager loading works. If that is the case, would I even benefit from any optimisation on the query at all if it is looping on blocks I do not need? As an example, I would normally do something like
{% set img = entry.content.type('images').first.images.first ?? null %}

Following on from whether this is worth it... I also want to get a text field on this matrix (from the first of a block type again), but, in another position in the template. Does this mean I need to do another loop again to get the data specifically for another field?
Is there a better way to deal with this?
I hope that makes sense, let me know if not I'll do my best to clarify.

**Tags:** eager-loading


---

## 2 answers

### Answer by username_1 on 2018-03-18. Score: 1

Additionally, it appears I cannot use the loop to save a variable to use outside of it

This is really a separate question. Answering it anyway: you need to give the variable some value before the loop, and then it works:
{% set img = null %} {# initial value #}

{% for block in entry.pageContent %}
  {% if block.type == 'images' %}
    {% if not img %}
      {% set img = block.images[0] ?? null %}
    {% endif %}
  {% endif %}
{% endfor %}

{# img will be the first image of first 'images' block, or null #}


---

### Answer by username_2 on 2017-10-19. Score: 0

This article may be helpful to you: [Speed up your Craft CMS Templates with Eager Loading](https://nystudio107.com/blog/speed-up-your-craft-cms-templates-with-eager-loading)

Basically, when you eager load the Matrix blocks, you're no longer getting an ElementCriteriaModel, rather you're getting an array pre-loaded with the Matrix blocks. That's why you can't do something like `entry.pageContent.type('images').first.images`


---

