Fallback filter

I wonder if with Jekyll 3.6 it is still necessary to for example do

controls: {% if site.controls != null %}{{ site.controls }}{% else %}true{% endif %}

instead of the much cleaner

controls: {{ site.controls | fallback: true }}

that has been offered in this PR but obviously never included but then sadly frozen-due-to-age. Is there any chance to get to see this functionality in Jekyll anytime soon? Is there a reason why this has not been integrated yet? Is there another way to get rid of this messy if-then-else glob?

1 Like

I have no idea what the fallback filter is, but Liquid has a default filter that works how you want. Replace fallback with default and you should be good to go.

controls: {{ site.controls | default: true }}

Thank you for the suggestion. I actually did try this in the first place, but did not get it to work as expected:

file content
_config.yml controls: false
default.tmpl controls: {{ site.controls | default: true }}
index.html controls: true
but:
_config.yml controls: false
default.tmpl controls: {{ site.controls }}
index.html controls: false

I have no explanation why implementing the default filter in the template turns out true, so I did some research and found the fallback PR and I figured Jekyll is doing something differently.

Do you have any clue, whats wrong with my settings?

The default filter seems to be working as expected, from the example you provided.

{{ site.controls | default: true }} means the result will default to true when site.controls evaluates to a “falsey”.

In the case of Liquid (and Ruby in general), both nil and false are the only “falsey” values. Everything else is a “truthy”.

One approach then would be to adjust your template to use a disabled setting.

{% unless site.controls == 'disabled' %}
  // controls are enabled by default.
  // markup related to controls here
{% endunless %}

yes, that totaly makes sense. thank you for the explanation :slight_smile: