Template Service

Use the template service to build templates that can be used to generate HTML mails or any other text-based format. Supports working with inputs, basic logic and localization/internationalization.

Basic functionality

Define templates

The following JSON example shows the definition of a Template:

{
    "name": "myTemplateName",
    "description": "Example Template Definition",
    "inputs": {
        "first_name": { "type": "string" }
    },
    "outputs": {
        "message": "Dear, {{@inputs.first_name}}"
    }
}

The inputs key allows you to define the inputs that can be provided to render a template.

The outputs key allows you to define the different outputs that the Template Service should render. You can reference inputs by using @inputs.<property> .

Under the hood, this service uses Handlebars as its template engine. For more information about how it can be used, see Helpers in Templates and Localizations in Templates, or consult the official Handlebars Guidearrow-up-right to learn how to write more complex templates

Create or update templates

The Extra Horizon CLIarrow-up-right has built-in tooling to make working with the Template Service very easy. Next to the simple example below, the CLI you can easily upload full HTML files as Template outputs, define Templates that extend other Templates or work with static/environment variables.

Simple example

Create a myTemplateName.json file with the same structure as the example Template definition file mentioned higher on this page.

To create and synchronise your Template, execute the following command:

The CLI has a lot more tools and features to help you manage your Templates! Visit the CLI documentationarrow-up-right for more information.

Render Templates

Rendering a Template results in a json that is returned with all variables filled in. For this we need to supply the Template Service with the variables defined in the inputs defined in the Template definition.

Using the example Template from the previous section, we can resolve it by calling something like:

With the above input the Template Service will return the following output:

Our Template is now resolved into a message that makes sense.

E-mail Templates

Templates that are defined in the Template Service can be easily used together with the Mail Service to create nicely formatted (HTML) e-mails.

To define e-mail Templates there are additional requirements on the outputs that must be present:

  • subject - the content of this output will be used as e-mail subject

  • body - the content of this output will be used as the body for the e-mail

circle-info

For more info how to send e-mails using Templates, take a look in the Mail Service documentationarrow-up-right

Last updated