• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Liquid question for else statement

Explorer ,
May 06, 2016 May 06, 2016

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

TOPICS
Developer

Views

361

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 06, 2016 May 06, 2016

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 08, 2016 May 08, 2016

Copy link to clipboard

Copied

LATEST

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines