Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T20:58:02.937Z
Answers.Text: 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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T20:25:08.020Z
Answers.LastActivityDate: 2014-08-13T20:25:08.020Z
Answers.UserDisplayName: Sean
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-29T05:35:20.040Z
Answers.Text: 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
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-28T16:18:52.710Z
Answers.LastActivityDate: 2014-07-28T16:27:25.653Z
Answers.LastEditDate: 2014-07-28T16:27:25.653Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Tags: <assets><forloop>
Title: Fetch Single asset without for loop?
ThreadText: # 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:
  
  ```twig
  {% 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()`](http://buildwithcraft.com/docs/templating/elementcriteriamodel#first) function to just get the first image:
  
  ```twig
  {% set image = block.backgroundImage.first() %}
  {{ image.getUrl() }}
  
  ```
  
  But this will fail if there is no image. So to be safe, you need:
  
  ```twig
  {% 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 username_4'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.  
  
  ---
  
  