> ## Documentation Index
> Fetch the complete documentation index at: https://developers.hubspot.jp/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# if文

> HubSpotのHubL if文に関する、CMS開発者向けの実践的なガイド。

モジュールやテンプレートに条件付きロジックを含めるには、HubLの[if文](#basic-if-statement-syntax)と[unless文](#unless-statements)を使用します。if文では、HubLで[サポートされる演算子](/cms/reference/hubl/operators-and-expression-tests)を使用して、[式の検証](/cms/reference/hubl/operators-and-expression-tests#expression-tests)を行うことができます。

<Warning>
  \*\*注：\*\*Eメールモジュールの条件文の中で[パーソナライズトークン](/cms/reference/hubl/functions#personalization-token)を使用する場合、[モジュールのプログラマブルEメールをオンにする](/cms/start-building/features/data-driven-content/emails-with-programmable-content)必要があります。

  [v3](/api-reference/marketing-marketing-emails-v3/guide)または[v4](/api-reference/marketing-single-send-v4/guide)の1回送信APIを介して渡される情報は、`if`文の中では機能しません。これは、情報が入る前にテンプレートがコンパイルされるためです。
</Warning>

## if文の基本構文

HubLでは、テンプレートのロジックの構築にif文を使用します。HubLのif文の構文は、Pythonの条件付きロジックと似ています。`if`文は[文の区切り文字](/cms/reference/hubl/variables-macros-syntax)に囲まれ、`if`文で始まり`endif`で終わります。

if文の基本構文の例を以下に示します。「condition」は、trueまたはfalseを評価するブール値の条件で置き換えます。

```hubl theme={null}
{% if condition %}
	If the condition is true print this to template.
{% endif %}
```

基本構文の次は、基本的なif文の実例を示します。以下の例に示すif文では、`my_module`という名前のHubLモジュールと`my_module`という名前の変数がテンプレートに含まれているかどうかをチェックしています。演算子を指定しないif文では、テンプレートのコンテキストにおいてモジュールが定義されているかどうかが評価されることに注意してください。

```hubl theme={null}
{% module "my_module" path="@hubspot/rich_text", label="My rich text module", html="Default module text" export_to_template_context=true %}

{% if widget_data.my_module %}
	A module named "my_module" is defined in this template.
{% endif %}

{% set my_variable = "A string value for my variable" %}
{% if my_variable %}
	The variable named my_variable is defined in this template.
{% endif %}
```

HubLモジュールの評価時には`if`文内のモジュール名が引用符で囲まれている一方で、変数の検証時には変数名が引用符で囲まれていない点に注意してください。上記の例では、モジュールも変数もテンプレート上に存在するため、文が評価された結果、マークアップが出力されます。これらの例では、モジュールと変数が定義済みかどうかだけが確認され、モジュールと変数に値が含まれているかどうかは確認されないことに注意してください。

次に、モジュールがテンプレート上に存在しているかどうかではなく、モジュールに値が含まれているかどうかを評価する`if`文について説明します。この場合は[export\_to\_template\_context](/cms/reference/modules/export-to-template-context)パラメーターを使用する必要があります。以下の例では、コンテンツエディター上でテキストモジュールに値が割り当てられている場合に、マークアップが出力されます。モジュールのテキストフィールドがクリアされている場合は、マークアップがレンダリングされません。カスタムモジュールについては、簡略化された`widget.widget_name`構文があります。この構文については[この例](/cms/reference/modules/configuration)で説明しています。

<CodeGroup>
  ```text input.txt theme={null}
  {% module "product_names" path="@hubspot/text", label="Enter the product names that you would like to render the coupon ad for", value="all of our products", export_to_template_context=True %}

  {% if widget_data.product_names.value %}
  <div class="coupon-ad">
  <h3>For a limited time, get 50% off {{ widget_data.product_names.value}}! </h3>
  </div>
  {% endif %}
  ```

  ```text output.txt theme={null}
  <div class="coupon-ad">
  <h3>For a limited time get 50% off all of our products!</h3>
  </div>
  ```
</CodeGroup>

## elifとelseの使用

追加の条件文や、1つ以上の条件がfalseの場合に実行されるルールを使用した高度な`if`文を作成できます。`elif`文を使用すると、直前の条件の後に評価される条件をロジックに追加できます。\*\*`else`\*\*文は、他の全ての条件がfalseの場合に実行されるルールを定義します。1つのif文の中で使用できる`elif`文の数に制限はありませんが、`else`文の使用は1つに限られます。

[`<=`演算子](/cms/reference/hubl/operators-and-expression-tests#comparison)を使用して変数の値をチェックするif文の基本構文の例を以下に示します。この例では、テンプレートで「Variable named number is less than or equal to 6.（変数numberは6以下です）」と出力されます。

```hubl theme={null}
{% set number = 5 %}

{% if number <= 2 %}
	Variable named number is less than or equal to 2.
{% elif number <= 4 %}
	Variable named number is less than or equal to 4.
{% elif number <= 6 %}
	Variable named number is less than or equal to 6.
{% else %}
	Variable named number is greater than 6.
{% endif %}
```

もう1つ、choiceモジュールを使用して、ユーザーが選択した部門に従って経歴ページの見出しをレンダリングする例を紹介します。この例では、choiceモジュールの特定の事前定義値のチェックに[== 演算子](/cms/reference/hubl/operators-and-expression-tests#comparison)を使用しています。

```hubl theme={null}
{% choice "department" label="Choose department", value="Marketing", choices="Marketing, Sales, Dev, Services" export_to_template_context=True %}

{% if widget_data.department.value == "Marketing" %}

<h3>Want to join our amazing Marketing team?!</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>

{% elif widget_data.department.value == "Sales" %}

<h3>Are you a Sales superstar?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>

{% elif widget_data.department.value == "Dev" %}

<h3>Do you love to ship code?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>

{% else %}

<h3>Want to work with our awesome customers?</h3>
<h4>We have exciting career opportunities on the {{ widget_data.department.value }} team.</h4>

{% endif %}
```

## unless文

`unless`文は、`if`文と似た条件文ですが、if文とは逆のロジックとして機能します。1つのブール値条件がtrueと評価されない場合に、初めのタグと終わりのタグで囲まれたコードがレンダリングおよびコンパイルされます。unless文は\*\*`unless`**で始まり**`endunless`で終わります。\*\*`unless`文では`else`を使用できますが、`elif`は使用できません。

以下の例では、リッチ テキスト フィールドに値が割り当てられていない場合に「Under construction」（準備中）という見出しが出力されます。リッチ テキスト フィールドに値が含まれている場合は、その値が表示されます。

```hubl theme={null}
{% module "my_page_content" path="@hubspot/rich_text", label="Enter your page content", html="" export_to_template_context=true %}

{{ widget_data.my_page_content.html }}

{% unless widget_data.my_page_content.html %}
<h1>This page is under construction.</h1>
<h3>Come back soon!</h3>
{% endunless %}
```

## ifchanged

HubLではif文とunless文の他に、`ifchanged`文があります。この文は、このタグを前回呼び出した後で特定の変数が変更された場合にだけマークアップをレンダリングする場合に使用します。

## インラインif文

HubLではインライン`if`文がサポートされています。[演算子と式の検証](/cms/reference/hubl/operators-and-expression-tests)による簡潔な条件ロジックを作成する際に使用できます。

```hubl theme={null}
{% set color = "Blue" if is_blue is truthy else "Red" %}     // color == "blue"

{{ "Blue" if is_blue is truthy else "Red" }}     // "Blue"

{% set dl = true %}
<a href="http://example.com/some.pdf" {{"download" if dl }} >Download PDF</a>
```

## 三項演算子

また[演算子と式の検証](/cms/reference/hubl/operators-and-expression-tests#logical)による条件文を簡単に記述する際には、三項演算子を使用することもできます。

```hubl theme={null}
// If the variable is_blue is true, output "blue", otherwise output"red"
{{ is_blue is truthy ? "blue" : "red" }}

// Set the variable is_red to false if is_blue is true, otherwise set to true
{% set is_red = is_blue is truthy ? false : true %}
```
