What are Conditional Statements?
Conditional statements let you include or exclude content in your email templates based on certain conditions. For example, you might want to send a special offer to subscribers who have been active for more than a year.
Using if
and else
helpers in your html template
Handlebars provides several built-in helpers for conditional logic:
{{#if condition}}
: Renders the block of code inside if the condition is true.{{else}}
: Renders the block of code if the precedingif
condition is false.
Here's an example JSON structure:
{
"isPremiumUser": true,
"qualifiesForDiscount": false
}
In your email template, you can use these helpers as follows:
{{#if isPremiumUser}}
<p>Welcome back, Premium User!</p>
{{else}}
<p>Welcome, please consider upgrading to our premium plan.</p>
{{/if}}
Example: Conditional statements in Semplates
For this example, we will stick to the example of a fictive banking company's communication material. We want to use a conditional statement to show a discount depending on the status of the user. To do so, we might decide to add a parameter qualifiesForDiscount to the payload. For tracking purposes, we additionally want to insert a personalized discount code as a nested attribute to our user. The payload might look like this:
{
"qualifiesForDiscount": true,
"user": {
"firstName": "Pablo",
"personalOutlink": "https://company.com/account/pablo1234",
"discountCode" : "annualPlanDiscount1234"
},
"recommmendedArticles": [
{
"heading": "Starting with ETFs",
"description": "Learn how to start with index funds today. Read our guide to up your ETF game",
"outlink": "https://company.com/blog/etf-guide-2023"
},
{
"heading": "The power of compounding interests",
"description": "Why time is the most powerful force in saving for retirement. See this comparison…",
"outlink": "https://company.com/blog/compounding-interest"
},
{
"heading": "Maximizing Tax Efficiency",
"description": "Discover strategies to minimize your tax burden while investing. Get the insights now.",
"outlink": "https://company.com/blog/tax-efficiency-2023"
}
]
}
Now we can head over to Semplates' Template Editor and start customizing our template. We use html blocks to hold the Handlebars Syntax and add the content that should be displayed when a customer is eligible for the discount:
AS you can see the template also contains blog content that we would like to show in case we do not want to show the promotion block. Now, when opening the Template Preview, you can add placeholders on the right site of the screen and review the rendered template on the left:
As you can see, conditional placeholders are rendered as a switch, which makes it super easy to turn them on and off to test how the template renders:
Make sure to test your placeholders thoroughly to ensure that they behave as expected. Incorrectly set conditions could lead to irrelevant or confusing messages.
Important Notes
Pro Tip: Switch to the Expert Mode (available on Premium plans and above), to edit the expected JSON structure directly. This view is especially valuable, when working with complex or large placeholder payloads, and, when aligning your SES API calls to your template designs.
With this guide, you're now ready to use basic conditional statements in your Semplates email templates. We also explore other advanced personalization features like nested attributes and iterating through lists.