真值与假值
所有数据类型必须返回 true 或 false。默认返回 true 的称为真值(truthy),默认返回 false 的称为假值(falsy)。
| 值 | 真值 | 假值 |
|---|---|---|
true | ✔ | |
false | ✔ | |
nil | ✔ | |
| 空字符串 | ✔ | |
0 | ✔ | |
| 整数 | ✔ | |
| 浮点数 | ✔ | |
| 数组 | ✔ | |
| 空数组 | ✔ | |
| 页面 | ✔ | |
| 空 object | ✔ |
示例
由于 nil 和 false 是仅有的两个假值,您需要谨慎处理 Liquid 中的值检查。某个值可能不是您期望的格式,但仍然为真值。
例如,空字符串是真值,因此需要使用 blank 检查其是否为空。EmptyDrop objects 也是真值,因此需要检查引用的 object 是否为 empty。
{% if settings.heading_font_family != blank %}
{{ settings.heading_font_family }}
{% else %}
No value for this setting has been selected.
{% endif %}
{% assign recipes_page_id = '3366359' %}
{% unless pages[recipes_page_id] == empty %}
{{ page[recipes_page_id].content }}
{% else %}
No page with this handle exists.
{% endunless %}
{
"settings": {
"heading_font_family": null
}
}
No value for this setting has been selected.
No page with this handle exists.