1

I have a single asset field that handles two kinds of images:

1) Big Banner image 2) Small featured image

The problem is I want to check for the presence of the second image before I output it on the page. I am doing it like so:

   {% if entry.igBanner[1] | length %}
      {% set asset = entry.igBanner[1] %}
      <img src={{ asset.url }} class="img-responsive" alt={{entry.title}}>
    {% endif %}

But when I try to check the length of an empty index (where the image is not ), my template crashes saying I am trying to pull a method on a null variable. How do I check the presence of an image asset at index[1]?

1 Answer 1

3

You could just check the length of the whole asset array first, to make sure there are two images. Something like this:

{% if (entry.igBanner | length) > 1 %}
    {% set asset = entry.igBanner[1] %}
    <img src={{ asset.url }} class="img-responsive" alt={{entry.title}}>
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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