<p>Image are returned as an array so could use either of the following:</p>

<p><code>{{ content.img[0].url }}</code></p>

<p>Or set a variable which fetches the asset to then output its fields</p>

<pre><code>{% set asset = content.img.first() %}
{{ asset.url }}
</code></pre>

<p>Related: <a href="https://craftcms.stackexchange.com/questions/12483/looping-through-images-in-a-asset-field-type">Looping through images in a Asset field type</a></p>

<hr>

<p><strong>UPDATE</strong>: The line <code>{% if content.img|length %}</code> will always be true because you are checking for existence of the field. You need to instead check that the field has a value using one of the methods below:</p>

<pre><code>{% for content in entry.contentBuilder.type('imgHeader') %}

    {% if content.img[0]|length %}
        {{ content.img[0].url }}
    {% else %}
        default jpg
    {% endif %}

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

<p>or</p>

<pre><code>{% for content in entry.contentBuilder.type('imgHeader') %}

    {% set image = content.img.first() %}

    {% if image|length %}
        {{ image.url }}
    {% else %}
        default jpg
    {% endif %}

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

CC BY-SA 3.0
2016-05-19T20:19:31.803Z
Hi, my version does display the image if it was assigned to the field. The problem is that the whole conditional don't work if there is no field added.
Mark H.
CC BY-SA 3.0
2016-05-19T13:02:25.823Z
2016-05-20T17:16:20.923Z
2017-04-13T12:51:00.200Z
Luke Pearce
<p>I'm trying to attach a custom header image for each entry, if none is selected then the default one will be displayed.</p>

<ol>
<li><code>contentBuilder</code> is the Matrix field.</li>
<li><code>imgHeader</code> is an assets block type.</li>
<li><code>img</code> is the actual field</li>
</ol>

<hr>

<pre><code>{% for content in entry.contentBuilder.type('imgHeader') %}

    {% if content.img|length %}
        {{ content.img.first.url }}
    {% else %}
        default jpg
    {% endif %}

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

<p>However this doesn't work. Please advise :)</p>

<matrix><conditional><block-types>
Matrix Field Conditional
# Matrix Field Conditional

(Asked by: username_0 on 2016-05-19)

I'm trying to attach a custom header image for each entry, if none is selected then the default one will be displayed.

contentBuilder is the Matrix field.
imgHeader is an assets block type.
img is the actual field


{% for content in entry.contentBuilder.type('imgHeader') %}

    {% if content.img|length %}
        {{ content.img.first.url }}
    {% else %}
        default jpg
    {% endif %}

{% endfor %}

However this doesn't work. Please advise :)

**Tags:** matrix, conditional, block-types

---

## 1 answers

### Answer by username_1 on 2016-05-19. Score: 2 | Accepted answer

Image are returned as an array so could use either of the following:
{{ content.img[0].url }}
Or set a variable which fetches the asset to then output its fields
{% set asset = content.img.first() %}
{{ asset.url }}

Related: Looping through images in a Asset field type

UPDATE: The line {% if content.img|length %} will always be true because you are checking for existence of the field. You need to instead check that the field has a value using one of the methods below:
{% for content in entry.contentBuilder.type('imgHeader') %}

    {% if content.img[0]|length %}
        {{ content.img[0].url }}
    {% else %}
        default jpg
    {% endif %}

{% endfor %}

or
{% for content in entry.contentBuilder.type('imgHeader') %}

    {% set image = content.img.first() %}

    {% if image|length %}
        {{ image.url }}
    {% else %}
        default jpg
    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: Hi, my version does display the image if it was assigned to the field. The problem is that the whole conditional don't work if there is no field added.  

---

