跳到主要内容

运算符

逻辑运算符与比较运算符

Liquid 支持与 conditional tags 配合使用的基本逻辑运算符和比较运算符。

运算符功能
==等于
!=不等于
>大于
<小于
>=大于或等于
<=小于或等于
or条件 A 或条件 B
and条件 A 且条件 B
contains检查字符串中是否包含某字符串或数组元素

contains

您可以使用 contains 检查数组或字符串中是否存在某个字符串。不能使用 contains 检查对象数组中是否存在某个 object。

{% if product.tags contains 'hot' %}
This potion contains hot properties.
{% endif %}
{
"product": {
"tags": [
"hot"
]
}
}
This potion contains hot properties.

运算顺序

在同一个 tag 中使用多个运算符时,运算符从左到右依次求值,且无法更改此顺序。

🚧 注意

括号 () 在 Liquid tags 中不是有效字符。如果尝试使用括号来分组运算符,则该 tag 将不会被渲染。

{% unless true and false and false or true %}
This evaluates to true, since Liquid checks tags like this:

((true and false) and false) or true
(false and false) or true
false or true
true
{% endunless %}
<!-- empty -->