• 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 hide if empty

Explorer ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

I have a field in a webapp and I only want to display the content (a link) if the field is populated. The webapp field is called 'barnesnoble'. The field contains a link. If there is a link present I want to display the list item, otherwise I want nothing to display.

This is what I've got

{% if {barnesnoble} !='' -%}

    <li><a href="{{barnesnoble}}"><img alt="Barnes and Noble" src="/images/logos/barnes-and-noble.jpg" style="border: 0px none;" /></a></li>

    {% endif -%}

I've tried using {{barnesnoble}} and barnesnoble in the if statement but it displays it regardless.

Can anyone help?

TOPICS
Developer

Views

914

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

correct answers 1 Correct answer

Participant , Mar 13, 2018 Mar 13, 2018

Depending on how you assigned the liquid variable barnesnoble, you might need to test its existence before its value

{% if barnesnoble and barnesnoble != '' -%}

Votes

Translate

Translate
Participant ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

Depending on how you assigned the liquid variable barnesnoble, you might need to test its existence before its value

{% if barnesnoble and barnesnoble != '' -%}

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
LEGEND ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

If it is a web app item field it will always output and have and empty value of some sort. The issue stems from if the item is a multiline with or without editor vs a string or other field type for example as it can likely have content or empty characters, html without content, /n etc.

The above though indicates a string field or the link custom field. What he has above I have in sites and 100% working the above Mike would be extra for experts but does not address what may be wrong. I would imagine the OP has not covered everything for us.

@debra the other thing as well is the name. If you named it barnesnoble because you do not know how to handle the spaces it needs to be considered as you would when rendering JSON

{{this['Barnes and Noble']}}

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
Explorer ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

Thanks - I will try that.

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
Explorer ,
Mar 13, 2018 Mar 13, 2018

Copy link to clipboard

Copied

Thanks - that was the problem. The field name was different in the web app. Good advice to check the content first.

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
Engaged ,
Mar 14, 2018 Mar 14, 2018

Copy link to clipboard

Copied

Another approach that I have used for the same sort of requirement:

{% capture link_size %}{{ "{tag_external link}" | size }}{% endcapture %}

    {% if  link_size  > 5 %}   

         ...

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
LEGEND ,
Mar 14, 2018 Mar 14, 2018

Copy link to clipboard

Copied

Sorry Bill but I defiantly not would recommend that.

- Using old tag when liquid is better.

- Capture is slow and should only be used in rare cases.

- That can not work as well.

The if will work and did not because of the name issue, just saying if exists as already suggested is an extra. All you need to do.

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
Engaged ,
Mar 14, 2018 Mar 14, 2018

Copy link to clipboard

Copied

Thanks Liam

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
Participant ,
Mar 14, 2018 Mar 14, 2018

Copy link to clipboard

Copied

FWIW, you’d typically see the symptom described in the OP because {% if <undefined> != "" %} is true.

As Liam says, testing for unassigned side-steps the issue of the missing variable and indicates the cause, but it's not necessarily the "correct" solution to to why it's missing.

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
LEGEND ,
Mar 14, 2018 Mar 14, 2018

Copy link to clipboard

Copied

LATEST

The common things with that for others who are reading while I mentioned but to outline..

- Old tags took names and made them all one word and used underscores

- Liquid is object data output similar to JSON so will output the names as they are in the admin fields

- One word is targeted normally {{word}} while multiple words need to be {{this['Multiple Words']}}

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