演算子
Liquid には多数の論理演算子と比較演算子が用意されています。演算子を使用すると、制御フロータグで論理を作成できます。
基本演算子
== |
等しい |
!= |
等しくない |
> |
より大きい |
< |
より小さい |
>= |
以上である |
<= |
以下である |
または |
論理和 |
かつ |
論理積 |
たとえば
{% if product.title == "Awesome Shoes" %}
These shoes are awesome!
{% endif %}
1つのタグで複数の比較を 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
は文字列のみを検索できます。オブジェクト配列内のオブジェクトを確認するために使用することはできません。
演算の順序
2つ以上の 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 %}