Template snippets
As you know, Mailkit templates are a bit different from how we usually imagine a template - they are not just customizable emails but a complex system giving the client access to highly sophisticated content personalisation functions using the template language. Most recently, we have added the option of creating snippets, i.e., bits of code that can be used across different templates.
From the very beginning, Mailkit has given its users the possibility to use complex template language to maximize the dynamic content of email campaigns. Likewise, there is no limit to how many templates you can have and how many files are included or processed in each template’s main file. However, there are in many situations in which you might find it useful to be able to share a specific part of the template’s code across different templates, making it easy to introduce changes into multiple templates. This is precisely what template snippets are for.
Imagine the most common situation - the header and footer of your messages. They are usually the same in all of your campaigns, or at least very similar. Why not define them once and for all, and then use them in different templates, saving time that would otherwise be spent on preparing, changing or completely redesigning them? In fact, there are many more such repeating elements, which are often highly costly in terms of preparation or maintenance: product boxes or such seemingly simple things as action buttons. We are now going to use the latter to demonstrate how snippets work.
Although these two buttons differ in colour, text and hyperlink, they can represent the same snippet, the form and content of which will be defined by certain parameters. Instead of including specific each button in the template using HTML, we place the code into a snippet, which can then be used in the template based on a set of parameters.
To do this, we create a file button.html in the template snippets, using the following code:
<a href="[% link -%]" target="_blank"
style="text-decoration: none!important; color: #ffffff;display:block;
font-weight: bold; padding: 10px 17px; font-family: Arial, Helvetica, sans-serif;
font-size: 16px; background-color:[% color -%]; ">
[% text -%]
</a>
As you can see, the snippet contains three variables - link, color and text - which have to be filled in. In the campaign’s main template file we then insert, whenever we wish to use the button, a call for the snippet to be included with the appropriate parameters and variables:
[% INCLUDE “button.html” link=> ‘http://www.sample.url’, color=> ‘#660033’, text=> ‘100 kč’ -%]
You might say that it would be no problem simply inserting a link directly into the template - yet then if you are trying to changing the look of the buttons, you will be forced to make adjustments in multiple places instead of just one. If you have more visually attractive buttons, for example by using a table with multiple cells, or various softening effects and the like, your code will be incomparably more complicated.
Snippets are obviously most advantageous in cases where a larger HTML block branches out in more complicated ways, for example when using product boxes, which can appear, with minor adjustments, across multiple campaigns and language versions. Preparing a single snippet and using it can then save hours of your time, which you would otherwise spend on creating individual templates, and even more on adjusting them. If you are creating a template or are looking for advice on how to optimize your existing templates, please get in touch with us.