default
为任何没有赋值的变量设置默认值。default
如果输入为 nil
、false
或为空,则会显示其值。
在此示例中,product_price
未定义,因此使用默认值。
输入
{{ product_price | default: 2.99 }}
输出
2.99
在此示例中,product_price
已定义,因此不使用默认值。
输入
{% assign product_price = 4.99 %}
{{ product_price | default: 2.99 }}
输出
4.99
在此示例中,product_price
为空,因此使用默认值。
输入
{% assign product_price = "" %}
{{ product_price | default: 2.99 }}
输出
2.99
允许 false
要允许变量返回 false
而不是默认值,可以使用 allow_false
参数。
输入
{% assign display_price = false %}
{{ display_price | default: true, allow_false: true }}
输出
false