Template Service V2

Overview

To future-proof the Template Service we've introduced a new major version update, which includes several structural and syntax changes. The Template Service documentation is already updated, but this guide will help you understand the changes and how to migrate your existing v1 templates to the new v2 format.

The migration to the new version can be done incrementally, allowing you to update your templates one at a time. To support this, the Mail Service is able to work with both v1 and v2 templates. When resolving a template, it will first look for a v2 template and, if that is not found, it will look for a v1 template.

We've worked on making this as easy as possible with the most recent changes to the Extra Horizon CLI. So we'll start by recommending installing the latest version of the CLIarrow-up-right to help you with the migration.

High level steps to migrate:

  • Duplicate Localizations with parameters

  • Migrate Templates

  • Update cluster settings

  • Update application code

  • Test thoroughly

  • Remove V1 Templates

In the sections below we will go into more detail on each of these steps, including how the CLI can be used to assist you in the migration.

Duplicate Localizations with parameters

If you are using localizations with parameters in your templates, you will need to update them. The new localization format is not compatible with the old one.

We recommend duplicating these localizations and updating the duplicates to use named parameters. This allows you to update your v2 templates to use the new localization format without breaking your existing templates that still rely on the old positional parameter format.

Localizations with parameters need to be updated from positional parameters (e.g. $1, $2) to named parameters (e.g. {{first_name}}, {{last_name}}). This requires creating new localization keys for the v2 templates, as the format of the parameters has changed.

From:

To:

Later when migrating your templates, you use the new localization key in your v2 templates, while your v1 templates can still use the old localization key.

Migrate Templates

When migrating your templates, you can do this incrementally, one template at a time. Start by creating a copy of your existing v1 template and updating the copy to conform to the v2 structure and syntax. This allows you to test the new template without affecting the existing one.

If you are not already using the CLI to manage your templates, we highly recommend doing so. See the CLI documentation for more information on how to use the CLI to manage your templatesarrow-up-right. You can also commit these template files to your version control system to keep track of (future) changes.

Furthermore, if you maintain multiple environments (such as development, staging, and production), you may need certain email templates to differ between them (for example, using environment-specific links). To address this, the CLI now supports variables for v2 templates. This feature lets you define variables within your templates and assign different values for each environment by providing environment variables to the CLIarrow-up-right.

Example

If you have a v1 template like this:

You can create a new template with the same name (or a different name if you prefer) and update it to the v2 format:

When creating the new v2 template, you can use the $schema property to point to the JSON schema for the template. This can help you catch any mistakes or missing properties in your template during development, as most code editors will use the schema to provide validation and auto-completion.

Below we will go into more detail on the specific changes that need to be made to migrate from v1 to v2 templates, including the structural changes and the syntax changes.

Structural Changes

Rename schema.fieldsinputs

The schema section of the template, where the input variables are defined, is now renamed to inputs to better reflect its purpose. Additionally, the root object is now implicitly assumed to be of type object, so there is no need to define a top-level type object anymore.

From:

to:

Rename fieldsoutputs

The fields section, where the outputs of the template are defined, is now renamed to outputs to better reflect its purpose.

From:

To:

Update the object type definitions

The object type definitions have been updated to align with a limited subset of JSON Schema. The fields keyword used to define the properties of an object is now replaced with properties.

From:

To:

Update the array type definitions

The array type definitions have been updated to align with a limited subset of JSON Schema. The type_configuration keyword used to define the type of items in an array is now replaced with items.

From:

To:

Remove all options arrays

We no longer support the more detailed validation options that were previously available in the options array. Any options arrays should be removed from your templates.

From:

To:

Replace deprecated types

The object_id and date types have been removed and should be replaced with the string type.

From:

To:

Templating Syntax Changes

The templating syntax has been updated from Velocity to Handlebars, which includes changes to how variables are accessed, how control structures are defined and how localization is handled.

Check out the main documentation for more information on the new syntax and handlebars helpers that are available in the v2 templates, but below we will highlight the most important changes that you need to be aware of when migrating your templates.

Input Access

Input variables can now be accessed using @inputs instead of $content.

The syntax also requires the use of double curly braces {{ }} to denote variable interpolation. Triple curly braces {{{ }}} can be used if you want to render HTML content within variables without escaping.

From:

To:

Control Structures

Control structures such as conditionals and loops have a different syntax in Handlebars compared to Velocity.

For example, an if statement in Velocity:

Would be written in Handlebars as:

Localizations

Localizations are now supported by the more widely used i18next library for a more complete and flexible internationalization solution. Localizations without arguments can be easily re-used by adding the t and the quotes inside the already present {{ }}.

For example:

Becomes:

As mentioned earlier, for localizations with arguments, we've recommended to create new v2 localizations in order to not break your v1 templates while migrating.

From a localization with positional parameters:

To a localization with named parameters:

Then, from a v1 template using the old localization:

To a v2 template using the new localization:

As mentioned before for variables with HTML content, if your localizations include HTML content, you can use triple curly braces to render the HTML without escaping it:

Update cluster settings

After migrating your templates to v2, you will need to update your cluster settings to point to the new template name/id for the default emails the user service sends out (e.g. password reset, email verification, etc.).

The CLI now has support for updating these settingsarrow-up-right. With the additional benefit of being able to target the templates by their name instead of their id, which should make it easier to update the settings without having to worry about the template ids being different in different environments.

Simply create a service-settings.json file with the settings you want to update, for example:

And then run exh settings sync to update the settings in your cluster.

Update application code

When you make use of templates in your application code, you will also need to update the code to use the new v2 templates. The nice thing is that the v2 templates can now also be targeted by their name instead of their id, which should make it easier to update the code without having to worry about the template ids being different in different environments.

circle-exclamation

When using the ExH SDK, you can update the usage of exh.templates.* to exh.templatesV2.* to start using the new v2 templates.

For example, if you were previously using:

You can update it to:

As mentioned, make sure the user has the RESOLVE_TEMPLATES permission to be able to resolve v2 templates.

Or if you are sending a mail like this:

You can update it to:

When using the Template Service API directly, similarly you can update the endpoints used from /templates/v1/ to /templates/v2/ and update the request body to match the new structure of resolving v2 templatesarrow-up-right.

Test thoroughly and Remove V1 Templates

Finally after you have migrated everything, don't forget to test everything thoroughly!

When you are happy with the results you can safely remove the v1 templates if you want to.

Last updated