> ## Documentation Index
> Fetch the complete documentation index at: https://foliant-help.iforiti.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Liquid 中的條件式內容

> 使用 if、unless 和 case，以及 Liquid 支援的比較運算，讓範本的部分內容只在某些訂單上列印。

Liquid 可以只在某個條件成立時列印範本的部分內容：例如已付款訂單的「已付款」印章、有禮品訊息時才顯示的禮品訊息，或寄往海外訂單的報關資料。在視覺化編輯器中，請改用區塊的「**顯示條件**」分頁（請參閱[只在某些訂單上顯示區塊](https://foliant-help.iforiti.com/zh-Hant/template-editor/block-conditions.md)）；本文適用於程式碼範本、「**自訂 Liquid**」區塊，以及區塊的「**Liquid 條件**」選項。

## if、elsif 和 else

```liquid theme={null}
{% if financial_status == 'paid' %}
  <p class="stamp">PAID</p>
{% elsif financial_status == 'partially_paid' %}
  <p>Partly paid. {{ 'total_due' | t }}: {{ total_price | minus: total_paid | money }}</p>
{% else %}
  <p>Payment pending</p>
{% endif %}
```

## unless

`unless` 的作用與 `if` 相反：

```liquid theme={null}
{% unless customer.tax_exempt %}
  {% include 'vat_breakdown' %}
{% endunless %}
```

## case

```liquid theme={null}
{% case shipping_address.country_code %}
  {% when 'US', 'CA' %}
    <p>Ships within North America</p>
  {% when 'GB' %}
    <p>Ships within the UK</p>
  {% else %}
    <p>International shipment</p>
{% endcase %}
```

## 比較運算

| 運算子 | 意義 | 範例 |
| - | - | - |
| `==` / `!=` | 等於／不等於 | `fulfillment_status == 'fulfilled'` |
| `>` `<` `>=` `<=` | 大於／小於 | `total_price >= 10000` |
| `contains` | 文字包含某內容，或清單包含某項目 | `tags contains 'Wholesale'` |
| `and` / `or` | 組合多個條件 | `pickup_in_store? or shipping_lines.size == 0` |
| `blank` | 空白或不存在 | `note != blank` |

Liquid 會由右至左解讀 `and` 和 `or`，且不支援括號。若規則較複雜，請將 `if` 標籤巢狀使用。

## 常見範例

只在訂單有備註時：

```liquid theme={null}
{% if note != blank %}<p>{{ note | escape }}</p>{% endif %}
```

只在訂單金額超過 100.00 時（金額以分為單位）：

```liquid theme={null}
{% if total_price >= 10000 %}<p>Free gift included</p>{% endif %}
```

只對標記為 VIP 的顧客：

```liquid theme={null}
{% if customer.tags contains 'VIP' %}<p>Thank you for being a VIP!</p>{% endif %}
```

只對寄往歐盟以外地區的訂單：

```liquid theme={null}
{% unless shipping_address.member_of_eu %}…customs details…{% endunless %}
```

只對草稿訂單：

```liquid theme={null}
{% if is_draft_order? %}…{% endif %}
```

## 視覺化區塊的 Liquid 條件

區塊的「**Liquid 條件**」就是放在 `{% if … %}` 裡的部分：單一運算式，不含標籤。例如 `total_price >= 10000 and customer.tags contains 'VIP'`。Foliant 會在您輸入時根據範例訂單進行檢查，並在 Liquid 無法解讀時通知您。

## 須知事項

* 文字比較會區分大小寫：`'vip'` 不會符合 `'VIP'`。視覺化編輯器中使用「**包含文字**」的標籤條件則不區分大小寫。
* `financial_status` 和 `fulfillment_status` 的值為小寫並使用底線：`paid`、`partially_refunded`、`unfulfilled`、`partially_fulfilled`。
* 請在預覽訂單選擇器中使用「**範例訂單**」和您自己的近期訂單來檢查條件。請參閱[預覽、紙張大小與縮放](https://foliant-help.iforiti.com/zh-Hant/template-editor/preview-paper-and-zoom.md)。

## 相關文章

* [只在某些訂單上顯示區塊](https://foliant-help.iforiti.com/zh-Hant/template-editor/block-conditions.md)
* [訂單物件與常用變數](https://foliant-help.iforiti.com/zh-Hant/template-editor/liquid-order-variables.md)
* [範本中的金額與幣別](https://foliant-help.iforiti.com/zh-Hant/template-editor/liquid-money-and-currency.md)
* [編輯範本的程式碼](https://foliant-help.iforiti.com/zh-Hant/template-editor/code-editor.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.