Answers.Body: <p>Related: here's article on my blog about generating placeholder images uing Twig macro:</p>
  
  <p><a href="http://craftsnippets.com/articles/placeholder-image-macro-for-craft-cms?s=s" rel="nofollow noreferrer">Placeholder image macro for Craft CMS
  </a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-29T11:33:00.983Z
Answers.LastActivityDate: 2020-03-29T11:33:00.983Z
Answers.UserDisplayName: Piotr Pogorzelski
Answers.Body: <p>Thanks to Brad for the answer, and the solution was to use <code>{% if entry.mainImage|length %}</code> instead of <code>{% if entry.mainImage %}</code> for the conditional. So my working code looked like this:</p>
  
  <pre><code>{% if entry.mainImage|length %}
    {% for asset in entry.mainImage %}
      &lt;img src="{{ asset.url }}" alt="{{ asset.title }}" /&gt;
    {% endfor %}
  {% else %}
    &lt;img src="http://placehold.it/130x130/f5f5f5/ccc.png" height="130" width="130" alt="{{ entry.title }}" /&gt;
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-20T01:34:21.087Z
Answers.LastActivityDate: 2015-01-20T01:34:21.087Z
Answers.UserDisplayName: Stephen
Body: <p>I'm trying to output an Asset image if the Main Image field is populated, and if it's not populated, I'd like to output a placeholder image. But I'm having problems getting the conditional to work - it outputs the image in the Assets field, but when the field is empty, it's not outputting the placeholder image. Any ideas on what I'm doing wrong here? Thanks.</p>
  
  <pre><code>{% if entry.mainImage %}
    {% for asset in entry.mainImage %}
      &lt;img src="{{ asset.url }}" alt="{{ asset.title }}" /&gt;
    {% endfor %}
  {% else %}
    &lt;img src="http://placehold.it/130x130/f5f5f5/ccc.png" height="130" width="130" alt="{{ entry.title }}" /&gt;
  {% endif %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-20T01:12:02.887Z
Comments.Text: Try `{% if entry.mainImage|length %}`
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-20T01:20:13.500Z
Comments.Text: Thanks Brad, that did the job. If you add that as an answer I can mark it as such, otherwise, I'll add it myself tomorrow and steal all your glory (and points). ;-)
Comments.UserDisplayName: Stephen
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-20T01:21:07.403Z
Comments.Text: Haha... all yours. I burn my reputation for warmth.
Comments.UserDisplayName: Brad Bell
Tags: <templating><assets><conditional>
Title: How to output a placeholder image if an Assets field is empty
ThreadText: # How to output a placeholder image if an Assets field is empty
  
  (Asked by: username_0 on 2015-01-20)
  
  I'm trying to output an Asset image if the Main Image field is populated, and if it's not populated, I'd like to output a placeholder image. But I'm having problems getting the conditional to work - it outputs the image in the Assets field, but when the field is empty, it's not outputting the placeholder image. Any ideas on what I'm doing wrong here? Thanks.
  
  ```
  {% if entry.mainImage %}
    {% for asset in entry.mainImage %}
      <img src="{{ asset.url }}" alt="{{ asset.title }}" />
    {% endfor %}
  {% else %}
    <img src="http://placehold.it/130x130/f5f5f5/ccc.png" height="130" width="130" alt="{{ entry.title }}" />
  {% endif %}
  
  ```
  
  **Tags:** templating, assets, conditional
  
  > **Comments:**
  > **username_1**: Try `{% if entry.mainImage|length %}`  
  > **username_0**: Thanks username_1, that did the job. If you add that as an answer I can mark it as such, otherwise, I'll add it myself tomorrow and steal all your glory (and points). ;-)  
  > **username_1**: Haha... all yours. I burn my reputation for warmth.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-01-20. Score: 7 | Accepted answer
  
  Thanks to username_1 for the answer, and the solution was to use `{% if entry.mainImage|length %}` instead of `{% if entry.mainImage %}` for the conditional. So my working code looked like this:
  
  ```
  {% if entry.mainImage|length %}
    {% for asset in entry.mainImage %}
      <img src="{{ asset.url }}" alt="{{ asset.title }}" />
    {% endfor %}
  {% else %}
    <img src="http://placehold.it/130x130/f5f5f5/ccc.png" height="130" width="130" alt="{{ entry.title }}" />
  {% endif %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2020-03-29. Score: 1
  
  Related: here's article on my blog about generating placeholder images uing Twig macro:
  
  [Placeholder image macro for Craft CMS](http://craftsnippets.com/articles/placeholder-image-macro-for-craft-cms?s=s)
  
  
  ---
  
  