HubL Reference

Table of contents
Close

If statements

The following article examines HubL if statements and unless statements. If statements often contain HubL supported operators and can be used to execute expression tests

Please note that using personalization variables (contact and company variables) within if statements is not currently supported for email in HubSpot. These variables can be used for logic on COS page and blog templates.

Basic if statement syntax

HubL uses if statements to help define the logic of a template. The syntax of HubL if statements is very similar to conditional logic in Python. If statements are wrapped in statement delimiters, starting with an opening if statement and ending with an endif.

The example below provides the basic syntax of an if statement, where "condition" would be replaced with the boolean rule that you were going to evaluate as being true of false.


{% if condition %}
If the condition is true print this to template.
{% endif %}

Now that you have seen the basic syntax, let's look at a few actual examples of basic if statements. The next examples below show if statements that check to see whether or not a HubL module with the name "my_module" and whether a variable named "my_variable" are present on a template. Notice that without any operators, the if statement will evaluate whether or not the module is defined in the context of the template.


{% rich_text "my_module" label='My rich text module', html='Default module text' %}
{% if "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 %}    
    

Notice that when evaluating the HubL module, the module name is left in quotes within the if statement and while testing the variable no quotes are used around the variable name. In both examples above, the module and the variable exist in the template, so the statements evaluate to print the markup. Please note that these examples are only testing whether the module and variable are defined, not whether or not they have a value.

Now let's look at a if statement that evaluates whether a module has a value, instead of evaluating whether it exists on the template. To do this, we need to use the export_to_template_context parameter. You can learn more about this parameter, here. In the example below, if the text module is valued in the content editor, the markup would print. If the module's text field was cleared, no markup would render.  If you are working within custom modules, there is a simplified widget.widget_name syntax outlined in the example here.


{% text "product_names" 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 %}
        
     

<div class="coupon-ad">
<h3>For a limited time get 50% off all of our products! </h3>
</div>  
    
    

Using elif and else

If statements can be made more sophisticated with additional conditional statements or with a rule that executes when the condition or conditions are false. Elif statements allow you to add additional conditions to your logic that will be evaluated after the previous condition. Else statements define a rule that executes when all other conditions are false. You can have an unlimited number of elif statements within a single if statement, but only one else statement.

Below is the basic syntax example of if statement that uses the <= operator to check the value of a variable. In this example, the template would print: "Varible named number is less than or equal to 6."


{% set number = 5 %}

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


Below is one more example that uses a choice module to render different headings for a careers page, based on the department chosen bythe user. The example uses the == operator, to check for certain predefined values in the choice module. 


{% 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 statements

Unless statements are conditionals just like if statements, but they work on the inverse logic. They will render and compile the code between the opening and closing tags, unless the single boolean condition evaluates to true. Unless statements begin with an unless and end with an endunless. They do not support elif or else

Below is an example that prints an "Under construction" header, unless the rich text field is valued. If the rich text field has content, then that content will display.


{% rich_text "my_page_content" 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

In addition to if and unless statements, HubL supports ifchanged statements. These statements can be used to only render markup when a variable has changed since a prior invocation of this tag.