<p>You can test if your <code>first()</code> method returns an asset model. 
And depending on that have different image URLs, the (transformed) asset's one or the URL of your placholder image.</p>

<pre><code>{% set image = block.mx_peoplePhoto.first() %}
{% set imageUrl = image ? image.url('500x500') : url('images/_static/placeholder.jpg') %}

&lt;img src="{{ imageUrl }}"&gt;
</code></pre>

<p>You could also consider to get that placeholder image from a <a href="http://buildwithcraft.com/docs/globals" rel="nofollow">global set</a>.</p>

CC BY-SA 3.0
2015-09-02T09:10:34.437Z
Thanks for taking a look.  Something very bizarre happening, it's only pulling through a replacement image for the most recently added entry??  All previous entries seem to be ignored.  I've cleared caches.
Martin
CC BY-SA 3.0
2015-09-01T20:55:59.103Z
2015-09-01T20:55:59.103Z
carlcs
<p>I have a macro that I use for all images that falls back to our logo if an image is not supplied to it for whatever reason.</p>

<pre><code>{% macro imageTransform(image, transform, caption, extraHTML) %}
{# Fallback to our logo if the image is null... #}
{% if not image|length %}
    {% set image = craft.assets.id('84').first() %}
{% endif %}
&lt;img src="{{ image.getUrl( transform ) }}" width="{{ image.getWidth( transform ) }}" height="{{ image.getHeight( transform ) }}" title="{{ caption|default('') }}" alt="{{ caption|default('') }}"{% if extraHTML %} {{ extraHTML }}{% endif %}&gt;    
{% endmacro %}  
</code></pre>

<p>And here is  a sample use of it</p>

<pre><code>{% for row in imagesTable %}
    &lt;div class="gallery-block__item"&gt;
        {% set image = row.image.first() %}             
        {% set caption = row.caption %}
        {% set transform = 'fit500By500' %}
        {% set extraHTML = "class=gallery-block__item-image" %}
        {{ imageLib.imageTransform( image, transform, caption, extraHTML) }}
        &lt;span class="gallery-block__caption"&gt;{{ row.caption }}&lt;/span&gt;
    &lt;/div&gt;
{% endfor %}
</code></pre>

<p>(Note the lack of quotes on the extraHTML BTW)</p>

CC BY-SA 3.0
2015-09-01T23:29:17.190Z
2015-09-02T03:52:24.803Z
2015-09-02T03:52:24.803Z
Jeremy Daalder
<p>I've looked at and tried a couple of solutions on here but struggling to apply them to my circumstance.</p>

<p>Below is my current code.  I would like a 'default' asset (image) to be used if one hasn't been specified by the admin.  This is so that the front end doesn't have gaps in the gallery.</p>

<pre><code>&lt;ul id="photListing"&gt;
{% cache %}
  {% for entry in craft.entries.section('people').find() %}
  &lt;li&gt;
    &lt;a href="{{ entry.url }}"&gt;
      &lt;figure&gt;
      {% for block in entry.basicProfileInfo %}
      {% set varBarrPic = block.mx_peoplePhoto.first() %}
        &lt;img src="{{ varBarrPic.url }}" alt=""&gt;
          &lt;figcaption&gt;&lt;span&gt;{{ block.mx_positionWithinBusiness }}&lt;/span&gt; {{ entry.title }}&lt;/figcaption&gt;
      {% endfor %}
      &lt;/figure&gt;
    &lt;/a&gt;
  &lt;/li&gt;
  {% endfor %}
{% endcache %}
&lt;/ul&gt;
</code></pre>

<p>I hope this is a simple solution.</p>

<p>Many thanks</p>

<p>Martin</p>

<assets><image>
Output a default asset if one hasn't been specified
# Output a default asset if one hasn't been specified

(Asked by: username_0 on 2015-09-01)

I've looked at and tried a couple of solutions on here but struggling to apply them to my circumstance.
Below is my current code.  I would like a 'default' asset (image) to be used if one hasn't been specified by the admin.  This is so that the front end doesn't have gaps in the gallery.

{% cache %}
  {% for entry in craft.entries.section('people').find() %}
  
    
      
      {% for block in entry.basicProfileInfo %}
      {% set varBarrPic = block.mx_peoplePhoto.first() %}
        
          {{ block.mx_positionWithinBusiness }} {{ entry.title }}
      {% endfor %}
      
    
  
  {% endfor %}
{% endcache %}


I hope this is a simple solution.
Many thanks
Martin

**Tags:** assets, image

---

## 2 answers

### Answer by username_1 on 2015-09-01. Score: 1

You can test if your first() method returns an asset model. 
And depending on that have different image URLs, the (transformed) asset's one or the URL of your placholder image.
{% set image = block.mx_peoplePhoto.first() %}
{% set imageUrl = image ? image.url('500x500') : url('images/_static/placeholder.jpg') %}



You could also consider to get that placeholder image from a global set.

> **Comments:**
> **username_0**: Thanks for taking a look.  Something very bizarre happening, it's only pulling through a replacement image for the most recently added entry??  All previous entries seem to be ignored.  I've cleared caches.  

---

### Answer by username_2 on 2015-09-01. Score: 0

I have a macro that I use for all images that falls back to our logo if an image is not supplied to it for whatever reason.
{% macro imageTransform(image, transform, caption, extraHTML) %}
{# Fallback to our logo if the image is null... #}
{% if not image|length %}
    {% set image = craft.assets.id('84').first() %}
{% endif %}
    
{% endmacro %}  

And here is  a sample use of it
{% for row in imagesTable %}
    
        {% set image = row.image.first() %}             
        {% set caption = row.caption %}
        {% set transform = 'fit500By500' %}
        {% set extraHTML = "class=gallery-block__item-image" %}
        {{ imageLib.imageTransform( image, transform, caption, extraHTML) }}
        {{ row.caption }}
    
{% endfor %}

(Note the lack of quotes on the extraHTML BTW)


---

