Template

The "Template" node is used to create and manipulate text templates. For example for generating HTML, configuration files, our other text based strings. The Template node allows you to generate dynamic content by injecting data into predefined templates using the Mustache templating language. When there's no need for dynamic templates, the Format can be set to Plain Text, which has a slight performance benefit.

How the Template node works

Input Data

You can connect the Template node to a source of data, such as an MQTT input, HTTP input, or any other node that provides data. This input data will be used to populate the template.

Template Definition

In the Template node configuration, you define the template using text based formatting languages like HTML with the Mustache syntax. Mustache is a simple and "logic-less" templating language that allows you to insert variables and expressions into your template.

For example, you can define a template like this: <p>Hello {{payload.name}}!</p>.

This example will output <p>Hello FlowFuse!</p>.

Output

When the Template node receives input data, it processes the data and replaces Mustache placeholders with the corresponding values from the input data. The resulting HTML or text is then sent as output to the next node in the flow.

Building JSON

The template node can be configured to parse the result of the input and template as JSON, to further use the message as an object, not as string.

Input data

{
payload: ['ACME', 1]
}

Template node configuration

  • Template:
    {
    "product": "{{payload.0}}",
    "version": {{payload.1}}
    }
  • Format: Mustache template
  • Output as: Parsed JSON

Output

{ "product": "ACME", "version": 1 }

Generating JavaScript object from YAML

Node-RED can also parse YAML

Input data

{
topic: 'FlowFuse',
payload: 3000
}

template node configuration

  • Template:
    options:
    title: {{topic}}
    port: {{payload}}
  • Format: Mustache template
  • Output as: Parsed JSON

Output

{ "title": "FlowFuse", "port": 3000 }

Comments

When a template gets larger, it might be useful to add comments to the template which will not appear in the output. Comments work about the same as the normal syntax, with a ! after the opening curly brackets:

{{! this won't show }}
This text will be in the output

Will result in This text will be in the output. Note there's no empty line character \n.

Mustache partials

Mustache by default supports partials to include. This is an unsupported feature in Node-RED.

Node Documentation

Sets a property based on the provided template.

Inputs

msg object
A msg object containing information to populate the template.
template string
A template to be populated from msg.payload. If not configured in the edit panel, this can be set as a property of msg.

Outputs

msg object
a msg with a property set by populating the configured template with properties from the incoming msg.

Details

By default this uses the mustache format, but this can be switched off if required.

For example, when a template of:

Hello {{payload.name}}. Today is {{date}}

receives a message containing:

{
  date: "Monday",
  payload: {
    name: "Fred"
  }
}

The resulting property will be:

Hello Fred. Today is Monday

It is possible to use a property from the flow context or global context. Just use {{flow.name}} or {{global.name}}, or for persistable store store use {{flow[store].name}} or {{global[store].name}}.

Note: By default, mustache will escape any non-alphanumeric or HTML entities in the values it substitutes. To prevent this, use {{{triple}}} braces.

If you need to use {{ }} within your content, you can change the characters used to mark the templated sections. For example, to use [[ ]] instead, add the following line to the top of the template:

{{=[[ ]]=}}

Using environment variables

The template node can access environment variables using the syntax:

My favourite colour is {{env.COLOUR}}.