操作符判断和计算(Operators)
Liquid 包含许多逻辑和比较操作符。你可以使用操作符通过 控制流 标签来创建逻辑。
基本操作符
== |
equals, 等于 |
!= |
does not equal, 不等于 |
> |
greater than, 大于 |
< |
less than, 小于 |
>= |
greater than or equal to, 大于或等于 |
<= |
less than or equal to, 小于或等于 |
or |
logical or, 或者,例如:标题包含a或者标题包含b |
and |
logical and, 并且,例如:标题不为空并且描述不为空 |
例如:
{% 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
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 %}