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?
Depending on how you assigned the liquid variable barnesnoble, you might need to test its existence before its value
{% if barnesnoble and barnesnoble != '' -%}
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 != '' -%}
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']}}
Copy link to clipboard
Copied
Thanks - I will try that.
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.
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 %}
...
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.
Copy link to clipboard
Copied
Thanks Liam
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.
Copy link to clipboard
Copied
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']}}