I know how to add images into my template:

{% set image = entry.featureImage.first() %}
<div class="imagewrapper">
    <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
</div>

But what if my client doesn't have feature images for all of his entries? I need some function that'll tell the website, if there is no image, omit <div class="imagewrapper">.

Perhaps some kind of if/else statement that I can use:

{% if
    //the featureimage for this entry is filled in
%}
    {% set image = entry.featureImage.first() %}
    <div class="imagewrapper">
        <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
    </div>
{% endif %}
up vote 9 down vote accepted

You can use the length Twig filter for that.

{% if entry.featureImage | length %}
    {% set image = entry.featureImage.first() %}
    <div class="imagewrapper">
       <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
    </div>
{% endif %}

If there isn't an image in featureImage then nothing will be output.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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