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

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

Closed
casey-chow opened this issue Jan 21, 2012 · 11 comments
Closed

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

casey-chow opened this issue Jan 21, 2012 · 11 comments

Comments

@casey-chow
Copy link

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

Ah. Thanks.

@dvdsgl
Copy link

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

fw42 commented Feb 11, 2015

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

@bristoljon
Copy link

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

fw42 commented Jul 12, 2017

There is an unless tag

@dyspop
Copy link

dyspop commented Jul 12, 2017

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

@bristoljon
Copy link

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

@anetacamo
Copy link

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

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.

@Ynjxsjmh
Copy link

Ynjxsjmh commented Mar 2, 2020

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

I do this by

{% if page.type == "asciidoc" %}
  {% include article/asciidoc.html %}
{% elsif page.type == "readtheorg" %}
  {% include article/readtheorg.html %}
{% elsif page.type == "markdown" or page.type == "" or page.type == nil %}
  {% include article/markdown.html %}
{% else %}
  "Post type invalid, your type is {{page.type}}, which is not a valid type."
{% endif %}

I think page.type == "" can check if type varaible is defined in page. If true, type is not defined.

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

No branches or pull requests

9 participants