<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>

CC BY-SA 4.0
2020-03-29T11:33:00.983Z
2020-03-29T11:33:00.983Z
Piotr Pogorzelski
<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>

CC BY-SA 3.0
2015-01-20T01:34:21.087Z
2015-01-20T01:34:21.087Z
Stephen
<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>

CC BY-SA 3.0
2015-01-20T01:12:02.887Z
Try `{% if entry.mainImage|length %}`
Brad Bell
CC BY-SA 3.0
2015-01-20T01:20:13.500Z
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). ;-)
Stephen
CC BY-SA 3.0
2015-01-20T01:21:07.403Z
Haha... all yours. I burn my reputation for warmth.
Brad Bell
<templating><assets><conditional>
How to output a placeholder image if an Assets field is empty
# 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 %}
    
  {% endfor %}
{% else %}
  
{% endif %}

**Tags:** templating, assets, conditional
> **Comments:**
> **username_1**: Try `{% if entry.mainImage|length %}`  
> **username_0**: 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). ;-)  
> **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 Brad 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 %}
    
  {% endfor %}
{% else %}
  
{% 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)


---

