liquid icon indicating copy to clipboard operation
liquid copied to clipboard

if condition on empty string

Open etienneroudeix opened this issue 13 years ago • 40 comments

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.

etienneroudeix avatar Jul 12 '13 13:07 etienneroudeix

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.

nickpearson avatar Jul 12 '13 14:07 nickpearson

Hi, thank you for your response ! Kind regards.

etienneroudeix avatar Jul 12 '13 14:07 etienneroudeix

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 :(

etienneroudeix avatar Jul 12 '13 14:07 etienneroudeix

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).

nickpearson avatar Jul 12 '13 14:07 nickpearson

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.

fw42 avatar Jul 12 '13 14:07 fw42

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

nickpearson avatar Jul 12 '13 14:07 nickpearson

I can't reproduce the bug. This:

{% assign my_variable = '' %}
{% if my_variable == '' %} works {% endif %}

works for me.

fw42 avatar Jul 12 '13 14:07 fw42

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?

fw42 avatar Jul 12 '13 14:07 fw42

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

etienneroudeix avatar Jul 12 '13 14:07 etienneroudeix

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.

etienneroudeix avatar Jul 12 '13 14:07 etienneroudeix

How about {% if my_variable == "" or my_variable == nil %}?

fw42 avatar Jul 12 '13 14:07 fw42

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 !

etienneroudeix avatar Jul 12 '13 15:07 etienneroudeix

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

chrishough avatar Apr 03 '14 07:04 chrishough

The != nil test works for me.

tonybrasunas avatar Dec 03 '14 22:12 tonybrasunas

It's a 17 monthes old issue. Maybe it's fixed now.

etienneroudeix avatar Dec 04 '14 08:12 etienneroudeix

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 avatar Jan 31 '15 23:01 apla

@apla Have you tried the Jekyll 3 beta? Jekyll 2 is still using Liquid 2.

parkr avatar Jan 31 '15 23:01 parkr

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?

rickydazla avatar Feb 04 '15 06:02 rickydazla

Github pages still use Jekyll 2.4.0. Would be great, if this issue would be fixed.

onsitediary-blog-de avatar Apr 12 '15 10:04 onsitediary-blog-de

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

onsitediary-blog-de avatar Apr 12 '15 10:04 onsitediary-blog-de