<p>Looking for the exact same answer I came across this question and would argue that joshangell's answer is the best if trying to simplify down from a for loop to one line of code.  The only issue is that the answer uses incorrect wrapping tags for an inline if statement.  So I submit this as the best answer for simplification and correct syntax:</p>

<pre><code>{{ block.backgroundImage | length ? block.backgroundImage.first().getUrl() : '' }}
</code></pre>

<p>Just used it in my own project and it worked like a charm.</p>

CC BY-SA 3.0
2014-08-13T20:58:02.937Z
Another idea to get it down to one line would be to wrap it into a [macro](http://twig.sensiolabs.org/doc/tags/macro.html). Then you can easily add things like retina versions or common CSS classes to the HTML output and still have a clean template code in one line.
carlcs
CC BY-SA 3.0
2014-08-13T20:25:08.020Z
2014-08-13T20:25:08.020Z
Sean
<p>You can call the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel#first"><code>first()</code></a> function to just get the first image:</p>



<pre class="lang-twig prettyprint-override"><code>{% set image = block.backgroundImage.first() %}
{{ image.getUrl() }}
</code></pre>

<p>But this will fail if there is no image. So to be safe, you need:</p>

<pre class="lang-twig prettyprint-override"><code>{% set image = block.backgroundImage.first() %}
{% if image %}
    {{ image.getUrl() }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-07-29T05:35:20.040Z
You could always use an inline conditional to get it down to one line, just for fun...: `{% block.backgroundImage | length ? block.backgroundImage.first().getUrl() : '' %}` :D
Josh Angell
CC BY-SA 3.0
2014-07-28T16:18:52.710Z
2014-07-28T16:27:25.653Z
2014-07-28T16:27:25.653Z
Marion Newlevant
<p>I've got a Assets field which is limited to 1 asset inside a Matrix field. Is there a better way to fetch the image without using a forloop?</p>

<p>At the minute I've got:</p>

<pre class="lang-twig prettyprint-override"><code>{% for image in block.backgroundImage %}
    {{ image.getUrl() }}
{% endfor %}
</code></pre>

<p>But it just doesn't seem right using a forloop when I know there is only one image.</p>

<p>Thanks</p>

<assets><forloop>
Fetch Single asset without for loop?
# Fetch Single asset without for loop?

(Asked by: username_0 on 2014-07-28)

I've got a Assets field which is limited to 1 asset inside a Matrix field. Is there a better way to fetch the image without using a forloop?
At the minute I've got:
{% for image in block.backgroundImage %}
    {{ image.getUrl() }}
{% endfor %}

But it just doesn't seem right using a forloop when I know there is only one image.
Thanks

**Tags:** assets, forloop

---

## 2 answers

### Answer by username_3 on 2014-07-28. Score: 8 | Accepted answer

You can call the first() function to just get the first image:
{% set image = block.backgroundImage.first() %}
{{ image.getUrl() }}

But this will fail if there is no image. So to be safe, you need:
{% set image = block.backgroundImage.first() %}
{% if image %}
    {{ image.getUrl() }}
{% endif %}

> **Comments:**
> **username_4**: You could always use an inline conditional to get it down to one line, just for fun...: `{% block.backgroundImage | length ? block.backgroundImage.first().getUrl() : '' %}` :D  

---

### Answer by username_1 on 2014-08-13. Score: 2

Looking for the exact same answer I came across this question and would argue that joshangell's answer is the best if trying to simplify down from a for loop to one line of code.  The only issue is that the answer uses incorrect wrapping tags for an inline if statement.  So I submit this as the best answer for simplification and correct syntax:
{{ block.backgroundImage | length ? block.backgroundImage.first().getUrl() : '' }}

Just used it in my own project and it worked like a charm.

> **Comments:**
> **username_2**: Another idea to get it down to one line would be to wrap it into a [macro](http://twig.sensiolabs.org/doc/tags/macro.html). Then you can easily add things like retina versions or common CSS classes to the HTML output and still have a clean template code in one line.  

---

