With snippets, you can reuse the same content across multiple templates. For example, a snippet might contain a repeatedly used header, call-to-action block or footer.
Editing a snippet causes all templates that reference it to receive the updated content. This is a more efficient workflow than manually editing the same content multiple times in multiple templates.
Table of contents
Creating or editing a snippet
To create or edit a snippet, follow these steps:
-
Navigate to Content > Snippets to bring up the Manage Snippets screen.
-
To create a new snippet, click Create New Snippet; to edit an existing snippet, click its name.
-
The Create a Snippet screen appears.
-
Provide a Snippet Name.
This name will be used to include the snippet in a template. It can contain letters, numbers, spaces, dashes, and underscores.
WARNING
Changing the name of an existing snippet breaks any template that references it by its old name. If you change a snippet's name, be sure to update all templates that rely on it.
-
To pass data to a snippet, use Positional Parameters.
A snippet's content might need to change based on the value of a user profile or event field.
For example, a snippet might select a product image based on a user profile's
favoriteColor
field.It's possible, though, that one template might want to consider the recipient's
favoriteColor
, while another needs to look at thelastPurchasedColor
. If the snippet is hard-coded to look at a particular field, it can't be re-used in both of these scenarios.To solve this problem, don't reference specific user profile or event fields in snippets. Instead, pass data to snippets as positional parameters: variables sent from the template to the snippet. Snippets that make use of positional parameters aren't tied to specific user profile or event fields and can be reused more broadly.
To add positional parameters to a snippet:
Click Add New Positional Parameter.
-
In the Key input, specify the parameter's name.
For example, specifying a Key of
firstName
allows the snippet to access any value passed to that parameter as{{firstName}}
.
Add as many positional parameters as needed.
Enter a Description that clarifies what the snippet is and where it should be used.
-
In the Markup input, specify the content of the snippet.
As you create or update a snippet's content, keep in mind the following:
The specific editor available for modifying snippet content depends on the Use WYSIWYG Template Editor setting in Settings > Project Settings.
For snippets that will be used in SMS messages, push notifications, or web push notifications, only use plain text (no HTML).
Changing an existing snippet impacts all templates that reference it (including those tied to live campaigns).
To reference a user profile field, event field, or positional parameter, use a Handlebars expression
such as {{userProfileField}}
or{{#if parameterName}}
.-
To reference a field from a data feed:
- Use double curly braces to reference data feed data. For example:
{{fieldInDataFeed}}
. - In the template that references the snippet, open the Advanced Options tab.
- On this tab, check Enable Template Generation Using Data Feeds and Merge the Data feed and User Contexts.
- Use double curly braces to reference data feed data. For example:
-
Typical CSS precedence rules apply to any CSS contained in a snippet:
- Inline CSS (with the HTML
style
attribute) overrides CSS rules in astyle
tag. - A more-specific selector takes precedence over a less-specific one.
- If two CSS rules have the same specifity, the one that appears later in the code takes precedence.
- A CSS rule with
!important
always takes precedence.
- Inline CSS (with the HTML
Close all HTML tags (unless you're intentionally leaving a tag open so that the template can close it).
Templates can make use of CSS styles defined in a snippet.
After editing the snippet, click Save. This creates (or updates) the snippet and allows templates to reference it.
Example snippet
The following properties describe a snippet that displays either a greyscale or color version of some social media icons, depending on the value of a passed-in positional parameter:
-
Snippet Name
socialMediaIcons
-
Positional Parameters
useColor
-
Description
Displays a greyscale or color collection of social media icons, depending on the boolean value passed to the useColor parameter.
-
Markup
{{#if useColor}} <br /> <img alt="Icons" src=".../socialMediaColor.png" /> <br /> {{else}} <br /> <img alt="Icons" src=".../socialMediaGreyscale.png" /> <br /> {{/if}
(source view)
Using a snippet in a template
To insert a snippet in a template, follow these steps:
-
Navigate to Content > Templates and create a new template or open an existing one.
-
On the Edit Template screen, click Insert Snippet.
-
In the Insert a Snippet window, select the snippet you'd like to insert.
-
If the snippet contains HTML, toggle on the Render HTML switch.
Enabling this option changes the Handlebars for inserting the snippet so that it uses triple curly braces rather than double curly braces. Triple curly braces cue Iterable to render the HTML in the template as HTML rather than as plain text.
-
If the snippet requires any positional parameter values, specify them in the Handlebars input, after the name of the snippet.
NOTE
When passing values to a snippet's positional parameters, templates can specify user profile fields, event fields, or literal values—all of which can have different types (numbers, strings, boolean values, etc.). Make sure to pass parameter values that make sense; for example, don't provide a string (such as a name) for a positional parameter that the snippet uses in a numeric comparison.
Click the Copy button.
-
In the appropriate place in your template, paste the text copied from the Insert a Snippet window.
-
To preview the template, including its snippet content, choose Preview > Preview With Data or Preview > Preview on Devices.
On the Preview on Devices screen, click Regenerate Previews to make sure you see up-to-date content.
Comments
0 comments
Please sign in to leave a comment.