運算子

Liquid 包含許多邏輯和比較運算子。您可以使用運算子,搭配控制流程標籤來建立邏輯。

基本運算子

== 等於
!= 不等於
> 大於
< 小於
>= 大於或等於
<= 小於或等於
邏輯或
邏輯和

例如

{% if product.title == "Awesome Shoes" %}
  These shoes are awesome!
{% endif %}

您可以在一個標籤中使用 andor 運算子進行多重比較

{% 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 只能搜尋字串。您不能使用它來檢查物件陣列中是否存在物件。

運算順序

在有多個 andor 運算子的標籤中,運算子會從右到左依序檢查。您無法使用括號來變更運算順序 — 括號在 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 %}