if condition on empty string
Hello, I was just wanting to test if my_variable was an empty string.
I'm using Liquid through Jekyll if it can help.
I tried :
{% if my_variable == '' %} I'm not empty {% endif %}
but no way.
In fact I have to do like this
{% if my_variable == %} I'm not empty {% endif %}
It sound ugly to me so I let you know.
By the way it's the same behaviour using case condition.
Thank you for your job on this rocking project.
You can use the blank keyword: {% if my_variable == blank %}, which internally checks my_variable.blank?. See the 'blank' entry in the LITERALS hash if you're curious about how it works.
Hi, thank you for your response ! Kind regards.
Well, I just tested :
{% if my_variable == blank %} I'm not empty {% endif %}
and
{% if my_variable.blank? %} I'm not empty {% endif %}
And none of them behave as it should :(
What is the exact output of this?
my_variable is {% if my_variable == blank %}blank{% else %}"{{ my_variable }}"{% endif %}
This will result in one of the following:
my_variable is blank
my_variable is "something"
Also, to make your logic match up, you'll either want to change == to != or remove "not" from "I'm not empty".
Sorry if I wasn't clear -- the .blank? is what Liquid uses internally (in its Ruby code) -- you can't actually call my_variable.blank? in your Liquid code (unless you've added a custom blank? Liquid method to whatever class my_variable is).
Shit, that might be a bug. "blank?" is not a Ruby thing but Rails. If you use Jekyll, that might not work. Not sure though.
True. empty is also in there (aliases to :empty?). @etienneroudeix, this is standard Ruby (doesn't depend on Rails), but it behaves different from blank?, so be careful when using it on strings:
# same
"".blank? #=> true
"".empty? #=> true
# different
" ".blank? #=> true
" ".empty? #=> false
I can't reproduce the bug. This:
{% assign my_variable = '' %}
{% if my_variable == '' %} works {% endif %}
works for me.
Do you want to test on the variable being the empty string or do you want to test on it being defined? Try testing for nil instead maybe?
WOW lot of passionate talking thank you.
my_variable is {% if my_variable == blank %}blank{% else %}"{{ my_variable }}"{% endif %} gave me :
my_variable is ""
and my_variable is {% if my_variable == empty %}empty{% else %}"{{ my_variable }}"{% endif %} gave me the same exactly.
I want to test an empty (not even a space) or unset string
my_variable is {% if my_variable == nil %}nil{% else %}"{{ my_variable }}"{% endif %} works fine when the variable is not define.
But I still only can test empty string like that : my_variable is {% if my_variable == %}blank or not set{% else %}"{{ my_variable }}"{% endif %}
Thanks
Well my bad !
my_variable is {% if my_variable == %}blank or not set{% else %}"{{ my_variable }}"{% endif %}
does not work with empty string. It just behave exactly like nil testing.
How about {% if my_variable == "" or my_variable == nil %}?
Excellent ! It works !
Thank you very much !
To let you know : if my_variable == blank or my_variable == nil does not works.
My problem is solved thank you !
In case anyone comes across this thread, I had to add 1 more piece to the conditional to make it work:
{% if post.image_thumbnail_500x500 And post.image_thumbnail_500x500 != "" And post.image_thumbnail_500x500 != nil %}
<img src="{{ post.image_thumbnail_500x500 | asset_path }}">
{% endif %}
The != nil test works for me.
It's a 17 monthes old issue. Maybe it's fixed now.
none of this really works in jekyll 2.5.4
{{ if page.xxx and page.xxx != "" and page.xxx != nil }}
<h1>crap!</h1>
{{ endif }}
@apla Have you tried the Jekyll 3 beta? Jekyll 2 is still using Liquid 2.
Is there a / what is the difference between nil, blank, and empty? Are they interchangeable? In Shopify Liquid is one preferable or are there situations where different ones should be used?
Github pages still use Jekyll 2.4.0. Would be great, if this issue would be fixed.
In meantime I use following workaround: {% capture site_info %}X{{ site.form-info-website }}{% endcapture %} {% if site_info == "X" %} <h1>form-info-website is not set</h1> {% endif %}