Copy link to clipboard
Copied
I have a custom web app field called image and I am trying to check if there is a value in it. If there is then display that image. If not then display a default image but it isn't working and I can't figure out why. I am just starting to work with liquid so any guidance would be appreciated!
Here is the code
{% if Image == "" %}
<span class="img-thumbnail"> <img alt="{{name}}" height="300" class="img-responsive" src="{{Image}}"> </span>
{% else %}
<span class="img-thumbnail"> <img alt="{{name}}" height="300" class="img-responsive" src="/img/rj-practitioners/team-1.jpg"> </span>
{% endif %}
Copy link to clipboard
Copied
Is the image field actually named 'image' ? Assuming that it is, the logic is as follows:
{% if image != '' -%}
Show this if image field is not empty ie !=
{% else -%}
Else do this if it indeed is empty.
{% endif -%}
Adding the '-' hyphen before the closing tag %} is just to remove unnecessary whitespace.
Thus:
{% if image != "" -%}
<span class="img-thumbnail">
<img alt="{{name}}" height="300" class="img-responsive" src="/img/rj-practitioners/team-1.jpg">
</span>
{% else -%}
<span class="img-thumbnail">
<img alt="{{name}}" height="300" class="img-responsive" src="{{image}}">
</span>
{% endif -%}
Copy link to clipboard
Copied
PS you could shorten this like:
{% if image == "" %}
{% assign image = "/img/rj-practitioners/team-1.jpg" -%}
{% endif %}
<span class="img-thumbnail">
<img alt="{{name}}" height="300" class="img-responsive" src="{{image}}">
</span>