template
Generate formatted text output using templates with variable substitution.
template Block
The template block sets a property based on the provided template.
Overview
The template block uses the mustache format by default, 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 MondayIt is possible to use a property from the flow context or global context. Just use {{flow.name}} or {{global.name}}, or for persistable 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:
{{=[[ ]]=}}Configuration Options
Template Format
Choose the output format for your template:
- Plain Text: Simple text output with variable substitution
- HTML: HTML-formatted output with proper escaping
- JSON: Structured JSON output
- JavaScript: JavaScript code generation
Template Syntax
The block supports Mustache-style templating syntax:
Basic Variables
Hello {{msg.payload.name}}!Nested Properties
User: {{msg.payload.user.firstName}} {{msg.payload.user.lastName}}Context Variables
Processed: {{flow.processedCount}} itemsConditional Content
{{#msg.payload.isActive}}
Status: Active
{{/msg.payload.isActive}}
{{^msg.payload.isActive}}
Status: Inactive
{{/msg.payload.isActive}}Loops
{{#msg.payload.items}}
- {{name}}: {{value}}
{{/msg.payload.items}}Common Use Cases
Email Templates
Generate formatted email content:
Subject: Document Processing Complete
Dear {{msg.payload.user.name}},
Your document "{{msg.payload.document.title}}" has been successfully processed.
Processing Details:
- Document ID: {{msg.payload.document.id}}
- Processed At: {{msg.payload.processedAt}}
- Status: {{msg.payload.status}}
{{#msg.payload.errors}}
Errors Found:
{{#.}}
- {{message}}
{{/.}}
{{/msg.payload.errors}}
Best regards,
RAPFlow SystemReport Generation
Create structured reports:
Processing Report - {{$now()}}
Summary:
- Total Documents: {{msg.payload.totalDocuments}}
- Successfully Processed: {{msg.payload.successCount}}
- Failed: {{msg.payload.errorCount}}
- Processing Time: {{msg.payload.processingTime}}ms
{{#msg.payload.documents}}
Document: {{title}}
- Status: {{status}}
- Confidence: {{confidence}}%
{{/msg.payload.documents}}API Response Templates
Format API responses:
{
"status": "{{msg.payload.status}}",
"message": "{{msg.payload.message}}",
"data": {
{{#msg.payload.data}}
"{{key}}": "{{value}}"{{^last}},{{/last}}
{{/msg.payload.data}}
},
"timestamp": "{{$now()}}"
}HTML Content
Generate HTML pages or fragments:
<!DOCTYPE html>
<html>
<head>
<title>{{msg.payload.title}}</title>
</head>
<body>
<h1>{{msg.payload.title}}</h1>
<div class="content">{{msg.payload.content}}</div>
{{#msg.payload.items}}
<div class="item">
<h3>{{title}}</h3>
<p>{{description}}</p>
</div>
{{/msg.payload.items}}
</body>
</html>Advanced Features
Built-in Functions
Use built-in template functions:
Current Time: {{$now()}}
Random Number: {{$random()}}
Message ID: {{$msgid()}}Custom Helpers
Define custom helper functions:
{{#formatDate}}
{{msg.payload.date}}
{{/formatDate}}Conditional Logic
Complex conditional content:
{{#msg.payload.user}}
Welcome back, {{name}}!
{{#isAdmin}}
You have admin privileges.
{{/isAdmin}}
{{^isAdmin}}
You have standard user access.
{{/isAdmin}}
{{/msg.payload.user}}
{{^msg.payload.user}}
Please log in to continue.
{{/msg.payload.user}}Tips
- Use meaningful variable names in your templates for better readability
- Test templates with sample data to ensure proper formatting
- Use HTML format for web content to ensure proper escaping
- Leverage conditional sections to handle optional content
- Use loops for dynamic lists and tables
- Keep templates simple and focused on single responsibilities
- Use the template editor's preview feature to test your templates
Notes
- Template variables are case-sensitive
- Missing variables will render as empty strings
- HTML format automatically escapes special characters
- JSON format validates the output structure
- The block preserves whitespace and formatting in templates
- Context variables are accessible using dot notation
Related Blocks
- change - For data preparation
- function - For complex data manipulation
- http response - For sending template output