5

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 %}
9

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