Written by © FolioDavid

Published on December 13, 2018, at 13:44:09

Liquid is not a programming language! Take care on the use of boolean values…

When dealing with Jekyll template and use of Liquid language one may take care when assing boolean expression to a variable: its does not work!!!!:

The assign tag only supports raw booleans (ie. true or false). Trying to assign a boolean expresion such as:

1
2
3
{% assign vars = 2 == 1 %} 

inexplicably assigns “2” to vars. To my knowledge, the only way is to use conditional statement to assign boolean values, such as follows:

1
2
3
4
5
6
{% assign enable_A=false %}
{% assign enable_B=true %}
{% if page.param1==true or page.param2==true  %}{%assign enable_A=true%}{% endif %}
{% if page.param2==false or enable_A==true    %}{%assign enable_B=false%}{% endif %}

Secondly, when mixing the use of boolean variable with the Liquiddefault filter, one may also take care on the ‘basic’ behavior of the filter. Basically in the following snippet code:

1
2
3
4
{% assign varA = false %}
{% assign vars = varA | default: true %}

vars is set to the default true value as the Liquiddefault filter will set the value only if “the left side is nil, false, or empty”.

参考文献