3

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.

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

I hope this is a simple solution.

Many thanks

Martin

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') %}

<img src="{{ imageUrl }}">

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

1
  • 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 Sep 2 '15 at 9:10
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 %}
<img src="{{ image.getUrl( transform ) }}" width="{{ image.getWidth( transform ) }}" height="{{ image.getHeight( transform ) }}" title="{{ caption|default('') }}" alt="{{ caption|default('') }}"{% if extraHTML %} {{ extraHTML }}{% endif %}>    
{% endmacro %}  

And here is a sample use of it

{% for row in imagesTable %}
    <div class="gallery-block__item">
        {% 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) }}
        <span class="gallery-block__caption">{{ row.caption }}</span>
    </div>
{% endfor %}

(Note the lack of quotes on the extraHTML BTW)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.