# Checking if a variable exists with strict\_variables set

**URL:** <https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207>\
**Category:** Help\
**Created:** [September 28, 2018, 11:46am UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207 "2018-09-28T11:46:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptoews](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/ptoews/32/1268_2.png) [@ptoews](https://talk.jekyllrb.com/u/ptoews)\
**Post date:** [September 28, 2018, 11:46am UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207/1 "2018-09-28T11:46:18Z")

</div>

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](https://github.com/Shopify/liquid/issues/89), 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](https://github.com/Shopify/liquid/issues/1034)

---

<div class="post-metadata">

**Author:** ![michaelbach](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelbach/32/2151_2.png) [@michaelbach](https://talk.jekyllrb.com/u/michaelbach)\
**Post date:** [June 10, 2020, 8:50am UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207/2 "2020-06-10T08:50:05Z")

</div>

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 `contains`operator might work. I do not know how; please advise.

Any help appreciated, best, Michael

---

<div class="post-metadata">

**Author:** ![michaelbach](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelbach/32/2151_2.png) [@michaelbach](https://talk.jekyllrb.com/u/michaelbach)\
**Post date:** [June 10, 2020, 10:49am UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207/3 "2020-06-10T10:49:21Z")

</div>

I just found a long thread on this here:

> <https://github.com/Shopify/liquid/issues/1034>
>
> I'm using a jekyll layout where I want to include content of pages, depending on whether it exists.
> Apparently, it is supposed...

  
but there’s no working solution.

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [September 7, 2020, 8:21pm UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207/5 "2020-09-07T20:21:25Z")

</div>

Regarding the original post you can use `default`

```auto
## {{ 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.

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [September 7, 2020, 8:33pm UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207/6 "2020-09-07T20:33:18Z")

</div>

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

> <https://github.com/Shopify/liquid/issues/223>
>
> Hello, I was just wanting to test if my\_variable was an empty string.
> I'm using Liquid through Jekyll if it can help.
> I...

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.

> **[Checking if a variable is set in Jekyll](https://www.mattcrampton.com/blog/checking_if_a_variable_is_set_in_jekyll/)**
>
> An easy way to check if a variable is set in Jekyll

```auto
{% 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.

```auto
{% if page.some_variable %}

```

---

<div class="post-metadata">

**Author:** ![vidhyav656](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/vidhyav656/32/1636_2.png) [@vidhyav656](https://talk.jekyllrb.com/u/vidhyav656)\
**Post date:** [June 10, 2021, 12:50pm UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207/7 "2021-06-10T12:50:19Z")

</div>

> [@MichaelCurrin](#):
>
> `{% if page.some_variable And page.some_variable != "" And page.some_variable != nil %}`

The “and” used between the condition should be in lower case.

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

```

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [June 10, 2021, 3:13pm UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207/8 "2021-06-10T15:13:28Z")

</div>

Thanks. That’s what happens when I copy paste.

By the way when you quote the forum, consider selecting just one line before clicking Quote, to avoid duplicating a wall of text.

---

<div class="post-metadata">

**Author:** ![vidhyav656](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/vidhyav656/32/1636_2.png) [@vidhyav656](https://talk.jekyllrb.com/u/vidhyav656)\
**Post date:** [June 10, 2021, 4:23pm UTC](https://talk.jekyllrb.com/t/checking-if-a-variable-exists-with-strict-variables-set/2207/9 "2021-06-10T16:23:34Z")

</div>

Thanks! Made the edit.

Your code did help me.
