Looking for some inspiration for how to personalize the messages you send with Iterable? This article describes some common personalization use cases and how to write the Handlebars code to accomplish them.
# In this article
# Create a CTA button with a dynamic link
It can be useful to create a CTA button that directs users to different places depending on their unique customer journey and/or preferences. To make the links in your CTAs dynamic, you can add merge tags to the CTA button code like this:
Example:
At send time, this code displays a "Shop now!" button that links to the user's
shoppingCart_url. To learn more about including merge tags in links, see
Dynamic Link Parameters.
# Randomize an email subject line
We all know how important subject lines are... but what if you can't choose just one? Well, maybe you don't have to! You can use a Handlebars block to randomly display one of three subject line options, like this:
This code displays one of three values for the subject line, depending on the send time (in seconds). Because send time will vary based on trigger time and processing time, this approach should lead to a nearly even distribution of subject line alternatives.
NOTE
If you want to measure differences in engagement across variations, use an experiment instead. The method described in this article does not record the variation used at send time.
To learn more about the Handlebars helpers used in this example, see Handlebars Reference: Math Helpers and Handlebars Reference: Date and Time Helpers.
# Display content if one of two fields contains a value
If you want to show some content based on a value that could exist in more than one user or event data field, you can use Handlebars to check both fields and display the content if the value is found in either field.
Example:
This code checks a user's profile to see if either the selected_city or city
field is set. If selected_city has a value, defaultIfEmpty returns it;
otherwise, it returns the value of city. This value is then compared to the
string "New York". If it matches, the block displays the text "I ❤️ New York".
If both selected_city and city are empty (or if either is set to a value
other than New York), no content is displayed.
To learn more about the Handlebars helpers used in this example, see Handlebars Reference: Conditional Logic Helpers.
# Create dynamic HTML styles
You can incorporate Handlebars code in your HTML styles to create personalized message elements like tables.
Example:
<table cellpadding="4" cellspacing="1" width="150px"> <thead> <tr> <th scope="col" style="background-color:#000099; color:#FFFFFF">Cat Names</th> </tr> </thead> <tbody> {{#ifGt cats.size 1}} {{#each cats}} {{#ifModEq @index 2 0}} <tr style="background-color:#0099CC; color:#FFFFFF"> <td> {{name}} </td> </tr> {{else}} <tr style="background-color:#FFFFFF; color:#000000"> <td> {{name}} </td> </tr> {{/ifModEq}} {{/each}} {{else}} {{#ifEq cats.size 1}} <tr style="background-color:#FFFFFF; color:#000000"> <td> {{cats.[0].name}} </td> </tr> {{/ifEq}} {{/ifGt}} </tbody> </table>
When rendered, this block displays a table of a user's cat names (as pulled
from a cats array on their user profile) with rotating background colors.
To learn more about the Handlebars helpers used in this example, see Handlebars Reference: Conditional Logic Helpers.
# Want to learn more?
For more information about some of the topics in this article, check out these resources. Iterable Academy is open to everyone — you don't need to be an Iterable customer!
Iterable Academy
Support docs