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

How do I check if a variable is defined? #89

Closed
casey-chow opened this Issue Jan 21, 2012 · 10 comments

Comments

Projects
None yet
8 participants
@casey-chow

casey-chow commented Jan 21, 2012

Say (in Jekyll, for example) I want to check if a certain variable, say header_image was defined and do something if it is, and something else if it isn't. How would I do that?

@tobi

This comment has been minimized.

Show comment
Hide comment
@tobi

tobi Jan 21, 2012

Member

why not check against nil. Nil and "doesn't exist" are the same concept in liquid (unlike JS).

{% if header_image %} .... {% endif %}

Member

tobi commented Jan 21, 2012

why not check against nil. Nil and "doesn't exist" are the same concept in liquid (unlike JS).

{% if header_image %} .... {% endif %}

@casey-chow

This comment has been minimized.

Show comment
Hide comment
@casey-chow

casey-chow Jan 24, 2012

Ah. Thanks.

casey-chow commented Jan 24, 2012

Ah. Thanks.

@casey-chow casey-chow closed this Jan 24, 2012

@dvdsgl

This comment has been minimized.

Show comment
Hide comment
@dvdsgl

dvdsgl Feb 11, 2015

What if the possibly undefined variable is a boolean, and you want to run the block if it's true or false, but not if it's undefined?

dvdsgl commented Feb 11, 2015

What if the possibly undefined variable is a boolean, and you want to run the block if it's true or false, but not if it's undefined?

@fw42

This comment has been minimized.

Show comment
Hide comment
@fw42

fw42 Feb 11, 2015

Member

then you can use {% if header_image == true or header_image == false %}

Member

fw42 commented Feb 11, 2015

then you can use {% if header_image == true or header_image == false %}

@bristoljon

This comment has been minimized.

Show comment
Hide comment
@bristoljon

bristoljon Jul 12, 2017

What about negating this check?

In my case the variable row.image may or may not be defined. If it is I want to show an <img> tag but hide a different background <div>.

Although {% if row.image %} works as expected, the only way I've found to negate it is:

{% if row.image %}
{% else %}
  <div>...</div>
{% endif %}

Is there a more succinct form e.g. {% if !row.image %}?

bristoljon commented Jul 12, 2017

What about negating this check?

In my case the variable row.image may or may not be defined. If it is I want to show an <img> tag but hide a different background <div>.

Although {% if row.image %} works as expected, the only way I've found to negate it is:

{% if row.image %}
{% else %}
  <div>...</div>
{% endif %}

Is there a more succinct form e.g. {% if !row.image %}?

@fw42

This comment has been minimized.

Show comment
Hide comment
@fw42

fw42 Jul 12, 2017

Member

There is an unless tag

Member

fw42 commented Jul 12, 2017

There is an unless tag

@dyspop

This comment has been minimized.

Show comment
Hide comment
@dyspop

dyspop Jul 12, 2017

the unless tag is the inverse of the if tag and sounds like it will work for you

dyspop commented Jul 12, 2017

the unless tag is the inverse of the if tag and sounds like it will work for you

@bristoljon

This comment has been minimized.

Show comment
Hide comment
@bristoljon

bristoljon Jul 13, 2017

Thanks @fw42 and @dyspop, that's exactly what I was looking for and it was in the docs right below if!

bristoljon commented Jul 13, 2017

Thanks @fw42 and @dyspop, that's exactly what I was looking for and it was in the docs right below if!

@anetacamo

This comment has been minimized.

Show comment
Hide comment
@anetacamo

anetacamo Nov 23, 2017

But if I want to check for a condition OR if the variable is not defined at all
lets say something like this


{% if row.number == 0 or !column.number  %}
something is missing
{% else %}
for loop
{% endif %}

I would prefer to write it in as few statements as possible because the code else if is large.
Is that possible? Thanks :)

anetacamo commented Nov 23, 2017

But if I want to check for a condition OR if the variable is not defined at all
lets say something like this


{% if row.number == 0 or !column.number  %}
something is missing
{% else %}
for loop
{% endif %}

I would prefer to write it in as few statements as possible because the code else if is large.
Is that possible? Thanks :)

@steinbachr

This comment has been minimized.

Show comment
Hide comment
@steinbachr

steinbachr Aug 13, 2018

For anyone who stumbles here and - like me - was looking for this check in the context of a Jekyll list filter (e.g. where_exp), I was able to eventually get it with where_exp: "item", "item.header_image != nil".

Unfortunately, it's pretty unclear from the language documentation what the difference between empty, nil, and blank is, so required some trial and error to get working.

steinbachr commented Aug 13, 2018

For anyone who stumbles here and - like me - was looking for this check in the context of a Jekyll list filter (e.g. where_exp), I was able to eventually get it with where_exp: "item", "item.header_image != nil".

Unfortunately, it's pretty unclear from the language documentation what the difference between empty, nil, and blank is, so required some trial and error to get working.

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