If Statements
You can include conditional logic in your modules and templates by using HubL if statements and unless statements. If statements often contain HubL supported operators and can be used to execute expression tests.
Please note: if you're using personalization tokens within a conditional statement of your email module, you must enable programmable email for the module.
Information passed via the v3 or v4 single send APIs will not function within if
statements, as the templates compile before the information populates.
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.
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_module
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.
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 an 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. In the example below, if the text module is valued in the content editor, the markup would print. If the module's text field were 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.
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: "Variable named number is less than or equal to 6."
Below is one more example that uses a choice module to render different headings for a careers page, based on the department chosen by the user. The example uses the == operator, to check for certain predefined values in the choice module.
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
. unless
statements support else
but not elif
.
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.
ifchanged
statements. These statements can be used to only render markup when a variable has changed since a prior invocation of this tag.if
statements. These can be used to write conditional logic in a concise manner with operators and expression tests.Thank you for your feedback, it means a lot to us.