Skip to content
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 · 40 comments
Closed

if condition on empty string #223

etienneroudeix opened this issue Jul 12, 2013 · 40 comments

Comments

@etienneroudeix
Copy link

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
Copy link
Contributor

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
Copy link
Author

Hi, thank you for your response !
Kind regards.

@etienneroudeix
Copy link
Author

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

fw42 commented Jul 12, 2013

I can't reproduce the bug. This:

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

works for me.

@fw42
Copy link
Contributor

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
Copy link
Author

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
Copy link
Author

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
Copy link
Contributor

fw42 commented Jul 12, 2013

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

@etienneroudeix
Copy link
Author

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
Copy link

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
Copy link

The != nil test works for me.

@etienneroudeix
Copy link
Author

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

@apla
Copy link

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
Copy link
Contributor

parkr commented Jan 31, 2015

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

@rickydazla
Copy link

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
Copy link

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

@onsitediary-blog-de
Copy link

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
Copy link

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
Copy link

7blink commented Oct 18, 2015

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

@OilSlick
Copy link

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
Copy link

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
Copy link

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
Copy link
Contributor

{% 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
Copy link

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
Copy link

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

@bdanin
Copy link

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

@syu-id
Copy link

syu-id commented Dec 8, 2017

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

@tomjoht
Copy link

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
Copy link

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
Copy link

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
Copy link
Contributor

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

@obendev
Copy link

obendev commented Dec 21, 2018

{% if include.banner.video == undefined %} worked for me
jekyll version 3.8.5

{% if include.banner.video %} works as well, if the variable isn't set, it will be false.

contolini added a commit to cfpb/design-system that referenced this issue Jun 23, 2020
It's very, very difficult to check a string for emptiness with Liquid.

Shopify/liquid#223 (comment)
contolini added a commit to cfpb/design-system that referenced this issue Jun 23, 2020
* Restructure DS website's information architecture

- Removes main nav and moves all nav elements to left-hand sidebar
- Changes nav categories
- Removes four netlify cms collections that were based on categories
  and replaces them with a single one called "Pages"
- Adds a netlify cms collection called "Navigation" that lets users
  edit the side nav
- Moves special pages like the homepage and the help page to a new
  collection called "Special pages"
- Updates tests and Jekyll/netlify metadata files as appropriate

https://GHE/CFPB/el-camino/issues/251

* Update website documentation and how-to-edit-stuff guide

* Remove the sample component page from the 'component' section

* Only show source tabs for fields with content

It's very, very difficult to check a string for emptiness with Liquid.

Shopify/liquid#223 (comment)

* Add sidebar to homepage

* Put anchor tags on own lines

Otherwise there's a space between the last word and the final period.
I could just put the last anchor tag on its own line but I don't want to
alienate it and make it feel uncomfortable.

* Fix BEM structure of side nav

* Properly check for empty sub categories in side nav
@gojimmypi
Copy link

gojimmypi commented May 15, 2021

I came here thinking I had a problem detecting blank or empty strings. When, in fact - I actually had a blank record in my authors.yml file, with an extra - that had not been commented out:

-
  id: gojimmypi
  name: gojimmypi
-
# warning: having only one author generates this message:
#  Liquid Exception: undefined method `[]' for nil:NilClass in author/gojimmypi/index.html
#             Error: undefined method `[]' for nil:NilClass
#             Error: Run jekyll build --trace for more information.
  # id: person2
  # name: gojimmypi

I tried doing something like this to detect the "blank" ID:

{% for siteauthor in site.data.authors %}
  {% if  siteauthor.size == "" or siteauthor.size == empty or siteauthor.size == blank  or siteauthor.size == nil or siteauthor.size < 1  %}
    WARNING: blank record found in authors.yml (stray ' character? single quotes are row delimiters!)
    {% assign the_author_id = "" %}
    {% assign the_author = "missing" %}
  {% else %}
    {% assign the_author_id = siteauthor.id  %}
    {% assign the_author = site.data.authors | where: 'id', siteauthor.id %}
  {% endif %}
{% endfor %}

But the siteauthor.id still causes this error:

gems/jekyll-3.9.0/lib/jekyll/filters.rb:323:in `block in item_property': undefined method `[]' for nil:NilClass (NoMethodError)

Lesson learned: never have a blank record with a stray - in the yml data file.

@vachana815
Copy link

{ % if settings.custom_message % }
Thank you. { { settings.custom_message } }
{ % endif % }
what would be the output of this one then ??

@oDinZu
Copy link

oDinZu commented Nov 1, 2022

Awesome, thanks for sharing guys and gals.

This worked for me when I am looping through paginator.posts. I had a display bug when the author data was empty.

{% if post == blank %}
{% comment %}
  Fix display bug when author data is empty.
{% endcomment %}
  {% else %}
    <div class="meta">
      <time class="published">{{ post.date | date_to_string }} {{ page.date | date_to_string }}</time>
      <h5 class="author"><img src="{{ author.avatar }}" alt="{{ author.first_name }} {{ author.last_name }}" /><span class="name">{{ author.first_name }}</span></h5>
    </div>
{% endif %}

@swshamiq
Copy link

not even a space

variable == nil worked the best

@MrHinsh
Copy link

MrHinsh commented May 30, 2023

my_variable is {% if my_variable == blank %}blank{% else %}"{{ my_variable }}"{% endif %}

include.category is {% if include.category == blank %}blank{% else %}"{{ include.category }}"{% endif %} results in:

include.category is ""

include.category is {% if include.category == nil %}blank{% else %}"{{ include.category }}"{% endif %} results in:

include.category is "blank"


I ended up with:

{% if include.category == or item.category contains include.category %}

Which works as expected but totally looks broken, but works.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests