运算符
Liquid 包含许多逻辑和比较运算符。您可以使用运算符通过 控制流 标签创建逻辑。
基本运算符
== |
等于 |
!= |
不等于 |
> |
大于 |
< |
小于 |
>= |
大于或等于 |
<= |
小于或等于 |
或 |
逻辑或 |
与 |
逻辑与 |
例如
{% if product.title == "Awesome Shoes" %}
These shoes are awesome!
{% endif %}
您可以使用 and
和 or
运算符在标签中进行多次比较
{% if product.type == "Shirt" or product.type == "Shoes" %}
This is a shirt or a pair of shoes.
{% endif %}
包含
contains
检查字符串内部是否存在子字符串。
{% if product.title contains "Pack" %}
This product's title contains the word Pack.
{% endif %}
contains
还可以检查字符串数组中是否存在字符串。
{% if product.tags contains "Hello" %}
This product has been tagged with "Hello".
{% endif %}
contains
只能搜索字符串。您不能使用它来检查对象数组中是否存在对象。
运算顺序
在包含多个 and
或 or
运算符的标签中,运算符按从右到左的顺序进行检查。您不能使用括号更改运算顺序 — 括号在 Liquid 中是无效字符,会阻止您的标签正常工作。
{% if true or false and false %}
This evaluates to true, since the `and` condition is checked first.
{% endif %}
{% if true and false and false or true %}
This evaluates to false, since the tags are checked like this:
true and (false and (false or true))
true and (false and true)
true and false
false
{% endif %}