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

{% if page.some_variable And page.some_variable != "" And page.some_variable != nil %}
   {{some_variable}}
{% 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

0