制御フロー
制御フロータグでは、Liquidコードのブロックを実行するかどうかを決定する条件を作成します。
if
特定の条件がtrue
の場合にのみコードブロックを実行します。
入力
{% if product.title == "Awesome Shoes" %}
These shoes are awesome!
{% endif %}
出力
These shoes are awesome!
unless
if
の逆: 特定の条件が満たされていない場合にのみコードブロックを実行します。
入力
{% unless product.title == "Awesome Shoes" %}
These shoes are not awesome.
{% endunless %}
出力
These shoes are not awesome.
次と等価です。
{% if product.title != "Awesome Shoes" %}
These shoes are not awesome.
{% endif %}
elsif / else
if
またはunless
ブロック内に条件を追加します。
入力
<!-- If customer.name = "anonymous" -->
{% if customer.name == "kevin" %}
Hey Kevin!
{% elsif customer.name == "anonymous" %}
Hey Anonymous!
{% else %}
Hi Stranger!
{% endif %}
出力
Hey Anonymous!
case/when
変数が指定された値を持つときに特定のコードブロックを実行するスイッチステートメントを作成します。case
はスイッチステートメントを初期化し、when
ステートメントはさまざまな条件を定義します。
when
タグは複数の値を受け入れることができます。複数の値が指定されている場合、変数がタグ内の値のいずれかと一致すると式が返されます。値はコンマ区切りの一覧として指定するか、または
演算子を使用して区切ります。
caseの最後にオプションのelse
ステートメントがあると、条件のいずれも満たされない場合に実行するコードが指定されます。
入力
{% assign handle = "cake" %}
{% case handle %}
{% when "cake" %}
This is a cake
{% when "cookie", "biscuit" %}
This is a cookie
{% else %}
This is not a cake nor a cookie
{% endcase %}
出力
This is a cake