Checking if a variable is set in Jekyll

Checking if a variable is set in Jekyll

This site is built using Jekyll which allows me to generate the entire thing as static html which can then be hosted on AWS S3. Working through the Jekyll documentation I couldn't find a way to check if a variable was set when coding in Jekyll's templating language Liquid.

This is easy in python and javascript but after some digging I found this Github issue thread which (after a lengthy discussion) came up with the following as best practice...

Pause reading for 5 seconds...
Join My Mailing List!
Note: I will never share your email with anyone else.
Awesome! Thanks so much.
Ok, lets continue.

Script to check if a variable is set in Jekyll

{% if page.some_variable And page.some_variable != "" And page.some_variable != nil %}
{% else %}
   some_variable is not set
{% endif %}

The "nil" check was specifically tripping me up. Hopefully this helps someone else out there.

Post A Twitter Response To This Blog Post

To: @mattccrampton