Checking if a variable exists with strict_variables set

Hi guys,

I have a layout that sets some headers dependent on the page variables.
Now, the <title> header should be set to page.title by default, but if the page has name defined in the front matter, this should be used instead. I already found out how to check if a variable exists, but this doesn’t work for me when strict_variables ist set to true.
I don’t want to disable this flag, because it’s really helpful to find mistakes while developing.

How can I solve this?

Edit: I’ve created an Issue on Github where I’ve shown the use case and a workaround for this case: https://github.com/Shopify/liquid/issues/1034

1 Like

I have the same problem and wanted to write a question, then hit on this. Old, no one rsponded… To revive, for concreteness, I have a front matter variable “language”. When this is set to “de”, fine, when omitted from front matter should default to “en” (and then inserted into the header).

{% if page.language != nil %}does not work, throws an error, and Jekyll stops processing.

Now, checking for existence of a variable not failing with strict_variables = true, one suggestion I found is this {% if page contains language %}, which does not work, whether language is quoted or not. So I thought to expand the page array into a string array, then the containsoperator might work. I do not know how; please advise.

Any help appreciated, best, Michael

I just found a long thread on this here:


but there’s no working solution.

Regarding the original post you can use default

## {{ page.title  | default: site.title }}

So if page title is not set then site title will be used for that page.

I believe that page.title and site.title and Jekyll’s builtin fields so they will always exist as valid fields

You might get a nil or empty string value if you didn’t set them.

You could also set title: '' or title: nil in your frontmatter if you wanted to include title.

As for using the strict check i haven’t used it before but it sounds like it will only give an error of a key does not exist

E.g. page.my_title. Or just plain {{ some_var }} that never got defined.

Which can help with variables that have typos or you forgot to define somewhere with a value.

So I think you can turn on the strict checker to catch mistakes and use the default flow to add a default so the title as my example.

That should allow the fallback and allow use of strict checks for safety.

Less about fallbacks and more about checking if value is set as per Michael’s response…

See this old 2013 thread

A ton if mixed answers using blank and empty and undefined and null and empty string. But they might be solving the empty string case and not the undefined case

See also this post about someone who had some success and linked to the above post.

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

I normally just use this if checking a boolean is true or if a string is not empty.

{% if page.some_variable %}