可用的 Liquid 條件運算子

在條件陳述式中使用時 (ifunless),某些 Liquid 值會視為 true,有些則視為 false。

在 Liquid 中,null 和布林值 false 會視為 false,其他項目則視為 true。 空字串、空陣列等會視為 true。 例如,

{% assign empty_string = "" %}
{% if empty_string %}
<p>This will render.</p>
{% endif %}

必要時,您可以測試空字串和陣列,使用特殊空值。

{% unless page.title == empty %}
<h1>{{ page.title }}</h1>
{% endunless %}

您也可以使用特殊大小屬性來測試 Liquid 類型

{% if page.children.size > 0 %}
<ul>
{% for child in page.children %}
<li>{{ child.title }}</li>
{% endfor %}
</ul>
{% endif %}

綜合

Operator
×
×
Null ×
String ×
空字串 ×
0 ×
1, 3.14 ×
陣列或字典 ×
空陣列或字典 ×
Object ×

請參閱