The purpose of this case is to demonstrate the filter “allow_false.” This is useful where we have assignments with default values, where we do not always want the default property to come into action.
There are often configurations within templates where we have an assignment which is specific at firm level. If we want the configurable variable to be equal to false - this will mean that by default, we don’t want something to happen in the template.
Case 1 displays the issues faced where previously assigned variables will always display as the default value and Case 2 will show us how the allow_false filter can overcome this.
In the example below, we will demonstrate how by default, we will display the gross profit percentage.
Case 1 - No filter
We initially would assign our variable to false, we can then see how this is displayed via the screenshots below.
{% assign display_option_gross_profit_percentage = false %}
Output display: {{ display_option_gross_profit_percentage }}
We can then assign the variable to itself with a default of true as seen below. In this instance, if the variable is falsy i.e. nil, false or empty, the default will come into play.
The problem we face with this syntax, is that the initial assignment, regardless of whether it is assigned as false, will always be true because of the default property of true.
{% assign display_option_gross_profit_percentage = false %}
Output display: {{ display_option_gross_profit_percentage }}
{% assign display_option_gross_profit_percentage = display_option_gross_profit_percentage | default:true %}
Output display: {{ display_option_gross_profit_percentage }}
Case 2 - With filter
We begin here in the exact same way as case 1, where we assign our variable to false.
Whenever we assign our variable to false, we can then use the allow_false filter as demonstrated below, which then allows us to use the configuration set to false, and the default property would not come into action here.
To enable this filter, we use: allow_false:true
Remember to add a comma (,) in between the default and allow_false filter, else this will stop your syntax working the way it should.
{% assign display_option_gross_profit_percentage = false %}
Output display: {{ display_option_gross_profit_percentage }}
{% assign display_option_gross_profit_percentage = display_option_gross_profit_percentage | default:true, allow_false:true %}
Output display: {{ display_option_gross_profit_percentage }}