New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if condition on empty string #223

Closed
etienneroudeix opened this Issue Jul 12, 2013 · 34 comments

Comments

Projects
None yet
@etienneroudeix

etienneroudeix commented Jul 12, 2013

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.

@nickpearson

This comment has been minimized.

Contributor

nickpearson commented Jul 12, 2013

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.

@etienneroudeix

This comment has been minimized.

etienneroudeix commented Jul 12, 2013

Hi, thank you for your response !
Kind regards.

@etienneroudeix

This comment has been minimized.

etienneroudeix commented Jul 12, 2013

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

@nickpearson

This comment has been minimized.

Contributor

nickpearson commented Jul 12, 2013

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

@fw42

This comment has been minimized.

Member

fw42 commented Jul 12, 2013

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.

@nickpearson

This comment has been minimized.

Contributor

nickpearson commented Jul 12, 2013

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
@fw42

This comment has been minimized.

Member

fw42 commented Jul 12, 2013

I can't reproduce the bug. This:

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

works for me.

@fw42

This comment has been minimized.

Member

fw42 commented Jul 12, 2013

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?

@etienneroudeix

This comment has been minimized.

etienneroudeix commented Jul 12, 2013

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

This comment has been minimized.

etienneroudeix commented Jul 12, 2013

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.

@fw42

This comment has been minimized.

Member

fw42 commented Jul 12, 2013

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

@etienneroudeix

This comment has been minimized.

etienneroudeix commented Jul 12, 2013

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 !

@chrishough

This comment has been minimized.

chrishough commented Apr 3, 2014

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

This comment has been minimized.

tonybrasunas commented Dec 3, 2014

The != nil test works for me.

@etienneroudeix

This comment has been minimized.

etienneroudeix commented Dec 4, 2014

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

@apla

This comment has been minimized.

apla commented Jan 31, 2015

none of this really works in jekyll 2.5.4

{{ if page.xxx and page.xxx != "" and page.xxx != nil }}
<h1>crap!</h1>
{{ endif }}
@parkr

This comment has been minimized.

Contributor

parkr commented Jan 31, 2015

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

@rickydazla

This comment has been minimized.

rickydazla commented Feb 4, 2015

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?

@onsitediary-blog-de

This comment has been minimized.

onsitediary-blog-de commented Apr 12, 2015

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

@onsitediary-blog-de

This comment has been minimized.

onsitediary-blog-de commented Apr 12, 2015

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

@yoshyosh

This comment has been minimized.

yoshyosh commented May 8, 2015

What is the way to do this in Jekyll 2?

Edit: I just check if the variable exists
{% if site_info %}
// Do things
{% endif %}

@7blink

This comment has been minimized.

7blink commented Oct 18, 2015

I have been using
{% if page.testVariable.size != 0 %}

@OilSlick

This comment has been minimized.

OilSlick commented Dec 6, 2015

I'm running Jekyll 3.0.1 and had to implement the "capture" workaround proposed by onsitediary-blog-de on April 12. I tried other solutions mentioned in this thread with no luck.

@edwinwright

This comment has been minimized.

edwinwright commented Feb 22, 2016

I found that if the variable has not been set, then I get no value back on the size calculation. To get around this I turned it into a truthy test which seems to be working fine. I'm running Jekyll 3.1.1

{% if component.status.size %}

@tatarjr

This comment has been minimized.

tatarjr commented Feb 13, 2017

In case anyone is still having this issue, try comparing against undefined. I've run into this with page.previous / page.next variables, and was able to solve it by comparing if it's undefined.

{% if my.variable == undefined %} ... {% endif %}

@dylanahsmith

This comment has been minimized.

Member

dylanahsmith commented Feb 13, 2017

{% if my_variable == '' %} is fine if you just want to check if the variable equals the empty string.

However, it sounds like the variable was actually nil and that is why that condition didn't work. In that case use {% unless my_variable %} to check if the variable is nil.

@RNCTX

This comment has been minimized.

RNCTX commented Jul 16, 2017

@edwinwright 's comment works for me on jekyll as well (3.1.6). Thanks!

In my case I was trying to find all files but exclude those without a front matter tag defined, in other words, filter out posts with an empty string like tags:

Nil, 0, undefined, and blank did not work.

thing.subthing.size != 0 does work.

@rickydazla

This comment has been minimized.

rickydazla commented Jul 16, 2017

Null, nil, empty, blank, undefined; possibilities are many, answers are æthereal and perhaps esoteric. ' '

@bdanin

This comment has been minimized.

bdanin commented Dec 5, 2017

<% if !thing.subthing.empty? %>not-empty: <%= thing.subthing %><% end %>
didn't see this exact answer in here, but this is what works for me in ruby middleman templates

@rongmu

This comment has been minimized.

rongmu commented Dec 8, 2017

{% if var and var != blank %} works for me.

@tomjoht

This comment has been minimized.

tomjoht commented Feb 17, 2018

I have a collection and am trying to use tags as a property in the frontmatter of pages (not posts) in this collection. I found that empty works to check whether the tags have any content. For example:

{% if page.tags == empty %}
there are no tags
{% else %}
there are page tags
{% endif %}

Again, I'm working with pages in a collection, not posts.

@sbliven

This comment has been minimized.

sbliven commented Mar 28, 2018

To summarize, here's a table of the boolean value of various values (jekyll 3.7.3, liquid 4.0.0):

Operator nil "" " "
var false true true
var == blank false false false
var == empty false true false
var == "" false true false
var == nil true false false

Generated with statements like {%if Operator %}true{% else %}false{% endif %}

  • Empty strings are "truthy", unlike in most languages
  • No operator returns true for both undefined (nil) and empty strings. Use 'or' as suggested above (var == nil or var == empty)
  • blank doesn't seem to work for strings. By analogy to the rails blank? operator, I would have expected both "" and " " to be blank, but liquid seems to only define it for certain custom datatypes.
@vinyanalista

This comment has been minimized.

vinyanalista commented Apr 20, 2018

I'm moving my blog from Jekyll 2.5.3 (Liquid 2.6.1) to Jekyll 3.8.0 (Liquid 3.0).

In my template, there was an if checking that variable1 and variable2 were undefined and defined, respectively. It was just as simple as:

{% if !variable1 and variable2 %}
    ...
{% endif %}

Now, Liquid complains about that:

Liquid Warning: Liquid syntax error (line 23): Unexpected character ! in "!variable1 and variable2" in search/index.html

See the new code (references are this issue and Checking if a variable is set in Jekyll):

{% if variable1 == nil or variable1 == empty %}
    {% if variable2 and variable2 != "" and variable2 != nil %}
        ...
    {% endif %}
{% endif %}

To make things even more verbose, parentheses are not permitted by Liquid, so necessarily I need two ifs to accomplish that task.

Does one believe that change was for good? If so, why?

@dylanahsmith

This comment has been minimized.

Member

dylanahsmith commented Apr 20, 2018

@vinyanalista that ! in {% if !variable1 and variable2 %} was never supported syntax, so was just being ignored by the lax parser. E.g.

require 'liquid'

source = <<-LIQUID
!true = {% if !true_var %}true{% else %}false{% endif %}
!false = {% if !false_var %}true{% else %}false{% endif %}
LIQUID
output = Liquid::Template.parse(source).render('true_var' => true, 'false_var' => false)
puts output

outputs

!true = true
!false = false

It works the same way on 2.6.1, so doesn't appear to be changed in liquid. I think the only difference is that jekyll is using the strict parser now which is catching your syntax error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment