Working with templates
Learn how to use the Knock template editor to design personalized cross-channel messages for your product.
Knock has a full suite of tools for building and managing your cross-channel message templates including:
- Rich personalization and control flow with Liquid.
- Reusable content blocks with partials.
- A rich, visual editor for building emails with pre-built and custom components.
- The ability to drill down into code for complex templating use cases.
- A live-preview pane for seeing how your template will look when sent to a recipient.
- A test runner for sending test messages.
- Localization and translation support with translations.
- Local or remote programmatic management through our CLI or Management API.
Overview
#Each channel step you add to a workflow or broadcast has its own message template. This template renders the message sent to your users on a given channel.
You can manage templates for each channel step via the Knock dashboard, or programmatically via the Management API.
Working with the template editor
#You can access a channel step's template by clicking the "Edit content" button.
Within the template editor you will always see:
- A state pane for setting the properties used to preview your template.
- The template editor itself, which will vary depending on the channel type.
- A preview pane for seeing how your template will look when sent to a recipient.
Version control
#All changes to templates are versioned through Knock's commit and environment model. Saved changes are not made "live" until they are committed and/or promoted to a higher environment.
You can view a text diff of any changes to template through the "View changes" button in the Changes tab as well as seeing who made the change and on what date.
Personalize messages with template variables
#To inject a variable into your template, enclose it with double curly braces: {{ a_variable }}.
You can use curly braces to reference a number of different variable types in your templates. We've included a few common types below.
You can find the full list of supported variables here.
Data payload variables
#All variables sent in the data payload of your workflow trigger call. If you send through { "a_variable": "something" } in your data payload you can reference this as {{ data.a_variable }} in your template.
User properties
#To reference a user property (such as name), use the recipient namespace. This looks up the recipient of a given notification, and then finds the specified property for that user. Here's a code example where a recipient's name and plan are injected into a notification template:
Full list of Knock variables available →
Adding control flow and iteration to your template
#The Knock template editor uses Liquid tags to create the logic and control flow for notification templates. To learn more about Liquid, you can check out their documentation.
Here are a few Liquid tag types that are commonly used in Knock notification templates.
If and else-if statements. For when you want to show different copy depending on a user property or a data variable from your trigger call. In the example below, we show different copy depending on whether a batch of comments includes one or many comments.
For loops. You can use Liquid's for...in... tag to iterate over a list of items. We can add this to our example from above to iterate over the comments in a batch and add each one to our notification.
There are also a number of Liquid filters you can use to mutate the variables you pass into a notification template. Here's an example that uses the split and first filters to pull the first name for a given user.
To learn more about the variables, Liquid keywords, and other helper functions available to you in the Knock template editor, check out our Liquid helper reference.
Sharing templates across channels
#You can use partials to share content across your templates, whether they belong to workflows, broadcasts, or guides.
Partials are reusable pieces of content that can include Liquid for rich templating. HTML partials can also be used as blocks within the visual email editor.
Learn more about working with partials.
Localization and translations
#Knock supports localization and translation of your message templates. You can use the t tag to wrap content you want to translate in your default language, and Knock will automatically generate translation files for you behind the scenes.
Learn more about working with translations.