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.
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.
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.
The “and” used between the condition should be in lower case.
{% if page.some_variable and page.some_variable != "" and page.some_variable != nil %}
{{some_variable}}
{% else %}
some_variable is not set
{% endif %}