最終更新日: 2025年8月28日
モジュールやテンプレートに条件付きロジックを含めるには、HubLのif文unless文を使用します。if文では、HubLでサポートされる演算子を使用して、式の検証を行うことができます。
**注:**Eメールモジュールの条件文の中でパーソナライズトークンを使用する場合、モジュールのプログラマブルEメールをオンにする必要があります。v3またはv4の1回送信APIを介して渡される情報は、if文の中では機能しません。これは、情報が入る前にテンプレートがコンパイルされるためです。

if文の基本構文

HubLでは、テンプレートのロジックの構築にif文を使用します。HubLのif文の構文は、Pythonの条件付きロジックと似ています。if文は文の区切り文字に囲まれ、if文で始まりendifで終わります。 if文の基本構文の例を以下に示します。「condition」は、trueまたはfalseを評価するブール値の条件で置き換えます。
{% if condition %}
	If the condition is true print this to template.
{% endif %}
基本構文の次は、基本的なif文の実例を示します。以下の例に示すif文では、my_moduleという名前のHubLモジュールとmy_moduleという名前の変数がテンプレートに含まれているかどうかをチェックしています。演算子を指定しないif文では、テンプレートのコンテキストにおいてモジュールが定義されているかどうかが評価されることに注意してください。
{% 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パラメーターを使用する必要があります。以下の例では、コンテンツエディター上でテキストモジュールに値が割り当てられている場合に、マークアップが出力されます。モジュールのテキストフィールドがクリアされている場合は、マークアップがレンダリングされません。カスタムモジュールについては、簡略化されたwidget.widget_name構文があります。この構文についてはこの例で説明しています。
{% 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 %}

elifとelseの使用

追加の条件文や、1つ以上の条件がfalseの場合に実行されるルールを使用した高度なif文を作成できます。elif文を使用すると、直前の条件の後に評価される条件をロジックに追加できます。**else**文は、他の全ての条件がfalseの場合に実行されるルールを定義します。1つのif文の中で使用できるelif文の数に制限はありませんが、else文の使用は1つに限られます。 <=演算子を使用して変数の値をチェックするif文の基本構文の例を以下に示します。この例では、テンプレートで「Variable named number is less than or equal to 6.(変数numberは6以下です)」と出力されます。
{% 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モジュールの特定の事前定義値のチェックに== 演算子を使用しています。
{% 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」(準備中)という見出しが出力されます。リッチ テキスト フィールドに値が含まれている場合は、その値が表示されます。
{% 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文がサポートされています。演算子と式の検証による簡潔な条件ロジックを作成する際に使用できます。
{% 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>

三項演算子

また演算子と式の検証による条件文を簡単に記述する際には、三項演算子を使用することもできます。
// 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 %}