<p>I think you need to use a <code>{% switch %}</code> statement like the following:</p>

<pre><code>{% switch matrixBlock.type %}

{% case "text" %}

    {{ matrixBlock.textField | markdown }}

{% case "image" %}

    {{ matrixBlock.image[0].getImg() }}

{% default %}

    &lt;p&gt;A font walks into a bar.&lt;/p&gt;
    &lt;p&gt;The bartender says, “Hey, we don’t serve your type in here!”&lt;/p&gt;
{% endswitch %}
</code></pre>

<p>You can read more here: <a href="http://buildwithcraft.com/docs/templating/switch" rel="nofollow">http://buildwithcraft.com/docs/templating/switch</a> </p>

CC BY-SA 3.0
2015-06-01T04:36:47.637Z
Right! This is actually what I was using (sorry I didn't add it before). To be more specific, I'm hung up on how it's only pulling the first image/asset and not the others. How do I avoid the `[0]` ?
Jeremy P. Beasley
CC BY-SA 3.0
2015-06-01T15:50:49.667Z
@JeremyPaulBeasley Looks like Mmikkel beat me to it but you need to do something like

{% for image in block.image %}
    <a href="{{ entry.url }}">
        <img data-lazy="{{ image.getUrl() }}" />
    </a>
{% endfor %}
Adam McCombs
CC BY-SA 3.0
2015-06-01T02:59:02.390Z
2015-06-01T02:59:02.390Z
Adam McCombs
<p>An Assets field always returns an instance of <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a> – essentially an array-like object – even if there's only 1 (or even 0) files in the field.</p>

<p>This is why things like <code>matrixBlock.image[0]</code> – or <code>matrixBlock.image.first()</code> – is needed to pull an actual asset, that you can call <code>getUrl()</code> and other related methods on.</p>

<p><em>To output all images in an Assets field, you need to</em> loop <em>over the field:</em></p>

<pre><code>{% for image in block.image %}
    &lt;a href="{{ entry.url }}"&gt;
        &lt;img data-lazy="{{ image.getUrl() }}" /&gt;
    &lt;/a&gt;
{% endfor %}
</code></pre>

<p>To put it all together, in this case it probably makes sense to <strong>filter</strong> the Matrix field using the <code>type</code> method, in order to only loop over the image blocks. This is especially useful if your Matrix block also contains other, non-image blocks – this way, you don't need the <code>switch</code> statement, and can get away with a minimal amount of code:</p>

<pre><code>{% set imageBlocks = entry.body.type('imageFull, imageHalf, imageCouple') %}
{% if imageBlocks | length %}
    {% for block in imageBlocks %}
        {% for image in block.image %}
        &lt;a href="{{ entry.url }}"&gt;
            &lt;img data-lazy="{{ image.getUrl() }}" class="{{ block.type }}" /&gt;
        &lt;/a&gt;
    {% endfor %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-06-01T05:53:18.790Z
2015-06-01T05:53:18.790Z
Mats Mikkel Rummelhoff
<p>I'm working on a basic portfolio site wherein each entry or "project" has a body Matrix field with various blocks for various types of images. I'd like to make an index of all projects showing one slideshow/carousel for each project containing all images within that project.</p>

<p>The problem is that I don't know how to query all images at once = All images, all blocks, in the order they appear in the entry.</p>

<p>Here's my specific code in use. It's almost there but due to the <code>0</code> in <code>image[0]</code> it's only pulling in the first image from each instance of each Matrix Block. For example, there are two instances of the block called <code>imageHalf</code> and it pulls the first image from each.</p>

<p>I don't understand how to avoid this integer....</p>

<pre><code>{% for block in entry.body %}

{% switch block.type %}

{% case "imageFull" %}

{% set image = block.image.total() %}

    &lt;a href="{{ entry.url }}"&gt;&lt;img data-lazy="
        {{ block.image[0].getUrl() }}
    " /&gt;&lt;/a&gt;

{% case "imageHalf" %}

    &lt;a href="{{ entry.url }}"&gt;&lt;img data-lazy="
        {{ block.image[0].getUrl() }}
    " /&gt;&lt;/a&gt;

{% case "imageCouple" %}

    &lt;a href="{{ entry.url }}"&gt;&lt;img data-lazy="
        {{ block.image[0].getUrl() }}
    " /&gt;&lt;/a&gt;

{% endswitch %}

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

<templating><matrix><field>
How to pull all images in [body] Matrix fields
# How to pull all images in [body] Matrix fields

(Asked by: username_0 on 2015-06-01)

I'm working on a basic portfolio site wherein each entry or "project" has a body Matrix field with various blocks for various types of images. I'd like to make an index of all projects showing one slideshow/carousel for each project containing all images within that project.
The problem is that I don't know how to query all images at once = All images, all blocks, in the order they appear in the entry.
Here's my specific code in use. It's almost there but due to the 0 in image[0] it's only pulling in the first image from each instance of each Matrix Block. For example, there are two instances of the block called imageHalf and it pulls the first image from each.
I don't understand how to avoid this integer....
{% for block in entry.body %}

{% switch block.type %}

{% case "imageFull" %}

{% set image = block.image.total() %}

    

{% case "imageHalf" %}

    

{% case "imageCouple" %}

    

{% endswitch %}

{% endfor %}

**Tags:** templating, matrix, field


---

## 2 answers

### Answer by username_1 on 2015-06-01. Score: 1

I think you need to use a {% switch %} statement like the following:
{% switch matrixBlock.type %}

{% case "text" %}

    {{ matrixBlock.textField | markdown }}

{% case "image" %}

    {{ matrixBlock.image[0].getImg() }}

{% default %}

    A font walks into a bar.
    The bartender says, “Hey, we don’t serve your type in here!”
{% endswitch %}

You can read more here: http://buildwithcraft.com/docs/templating/switch

> **Comments:**
> **username_0**: Right! This is actually what I was using (sorry I didn't add it before). To be more specific, I'm hung up on how it's only pulling the first image/asset and not the others. How do I avoid the `[0]` ?  
> **username_1**: @JeremyPaulBeasley Looks like Mmikkel beat me to it but you need to do something like  {% for image in block.image %}     <a href="{{ entry.url }}">         <img data-lazy="{{ image.getUrl() }}" />     </a> {% endfor %}  

---

### Answer by username_2 on 2015-06-01. Score: 1

An Assets field always returns an instance of ElementCriteriaModel – essentially an array-like object – even if there's only 1 (or even 0) files in the field.
This is why things like matrixBlock.image[0] – or matrixBlock.image.first() – is needed to pull an actual asset, that you can call getUrl() and other related methods on.
To output all images in an Assets field, you need to loop over the field:
{% for image in block.image %}
    
        
    
{% endfor %}

To put it all together, in this case it probably makes sense to filter the Matrix field using the type method, in order to only loop over the image blocks. This is especially useful if your Matrix block also contains other, non-image blocks – this way, you don't need the switch statement, and can get away with a minimal amount of code:
{% set imageBlocks = entry.body.type('imageFull, imageHalf, imageCouple') %}
{% if imageBlocks | length %}
    {% for block in imageBlocks %}
        {% for image in block.image %}
        
            
        
    {% endfor %}
{% endif %}


---

