控制流標(biāo)記(control flow tag)能夠根據(jù)編程邏輯改變 Liquid 輸出的信息。
只有當(dāng)某個條件為?true
?時才執(zhí)行一段代碼。
輸入
{% if product.title == 'Awesome Shoes' %}
These shoes are awesome!
{% endif %}
輸出
These shoes are awesome!
與?if
?相對 – 只有當(dāng)某個條件不成立時才執(zhí)行一段代碼。
輸入
{% unless product.title == 'Awesome Shoes' %}
These shoes are not awesome.
{% endunless %}
輸出
These shoes are not awesome.
和如下實例的執(zhí)行結(jié)果一致:
{% if product.title != 'Awesome Shoes' %}
These shoes are not awesome.
{% endif %}
為?if
?或?unless
?添加更多狀態(tài)判斷。
輸入
<!-- If customer.name = 'anonymous' -->
{% if customer.name == 'kevin' %}
Hey Kevin!
{% elsif customer.name == 'anonymous' %}
Hey Anonymous!
{% else %}
Hi Stranger!
{% endif %}
輸出
Hey Anonymous!
創(chuàng)建一個開關(guān)表達(dá)式,用于將一個變量和多個不同值進行比較。case
?用于初始化一個開關(guān)表達(dá)式,when
?用于比較他們的值。
輸入
{% assign handle = 'cake' %}
{% case handle %}
{% when 'cake' %}
This is a cake
{% when 'cookie' %}
This is a cookie
{% else %}
This is not a cake nor a cookie
{% endcase %}
輸出
This is a cake
? Copyright 2023 深圳藍(lán)曬科技有限公司. 粵ICP備2023054553號-1