# Render Template

> Generates dynamic text using the Handlebars templating engine.

Source: https://docs.sommatic.ai/en/nodes/data.template/

- Slug: `data.template`
- Family: Data
- Version: 1.0.0
- Status: Available
- Execution: sync

Generates dynamic text using the Handlebars templating engine.

This operator allows you to merge data variables into a text template. It is ideal for constructing emails, SMS messages, HTML documents, or SQL queries.

**Features:**
- **Handlebars Syntax:** usage of `{{variable}}`, `{{#each list}}`, `{{#if condition}}`.
- **Rich Input:** Full access to the payload as context.

**Use Cases:**
- Creating a personalized email body: "Hello {{user.name}}..."
- Constructing a complex file path: "/archives/{{year}}/{{month}}/report.pdf"

## Configuration

- `template` (string, required): The text template using Handlebars syntax.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): Rendered
- `error`: Error

## Input contract

- `payload` (object, optional): Data context available in the template.

## Output contract

- `result` (string, optional): The rendered string.

## In expressions

What a later node can read from this node's output with `$("Render Template").item.json.<path>` (write your node's label instead of `Render Template`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Render Template").item.json.result` (string): The rendered string. **The worker does not return it: reads as undefined.**
- `$("Render Template").item.json.customer` (object) = `{…}` (Not suggested by the editor)
- `$("Render Template").item.json.customer.name` (string) = `"Ana"` (Not suggested by the editor)
- `$("Render Template").item.json.order_id` (string) = `"ord_123"` (Not suggested by the editor)
- `$("Render Template").item.json.text` (string) = `"Hello Ana, your order ord_123 is ready."` (Not suggested by the editor)


## How it works

Renders a Handlebars template against the incoming payload and adds the rendered text as `text`.

1. Reads `template` raw from the config: it is not passed through the Expression Engine, so every `{{ }}` in it belongs to Handlebars. Other config keys are resolved as usual.
2. Compiles it with Handlebars 4 and renders it with the payload as context: `{{customer.name}}`, `{{#each items}}...{{/each}}`, `{{#if paid}}...{{/if}}`.
3. Returns the payload plus `text` through `out`.

## In a workflow

### Node

```json
{
  "node_id": "node-ready-message",
  "label": "Ready message",
  "operator_slug": "data.template",
  "operator_version": "1.0.0",
  "config": {
    "template": "Hello {{customer.name}}, your order {{order_id}} is ready."
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "customer": {
      "name": "Ana"
    },
    "order_id": "ord_123",
    "text": "Hello Ana, your order ord_123 is ready."
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The template rendered.

```json
{
  "customer": {
    "name": "Ana"
  },
  "order_id": "ord_123",
  "text": "Hello Ana, your order ord_123 is ready."
}
```

### `error`

The template has a syntax error.

```json
{
  "ok": false,
  "errors": [
    "Template Error: Parse error on line 1: ..."
  ],
  "port": "error"
}
```

## Behaviour in the code

- `template`: Only payload fields are visible. Expression Engine variables (`$json`, `$("Node")`, `$now`, `$execution`) do not exist here: write `{{order_id}}`, not `{{ $json.order_id }}`. An empty template renders `""`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Template Error: Parse error on line <n>: <details>` | `error` | Invalid Handlebars syntax, such as an unclosed `{{#if}}` block. |

## Notes and limits

- `{{ }}` escapes HTML characters (`&`, `<`, `>`, quotes); use `{{{ }}}` for raw output.
- Only the built-in Handlebars helpers are available (`if`, `unless`, `each`, `with`, `lookup`, `log`); no custom helpers are registered. Missing fields render as empty text.
- The output key is `text` (the output contract says `result`); an existing `text` key is overwritten.
- When the envelope has no `data` (or `data` is falsy), the engine stores the whole envelope as the node output: on `error` the next node receives `{ ok: false, errors: [...], port: "error" }` as its payload.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "data.template",
  "name": "Render Template",
  "category": "data",
  "version": "1.0.0",
  "description": "Generates dynamic text using the Handlebars templating engine.\n\n  This operator allows you to merge data variables into a text template. It is ideal for constructing emails, SMS messages, HTML documents, or SQL queries.\n\n  **Features:**\n  - **Handlebars Syntax:** usage of `{{variable}}`, `{{#each list}}`, `{{#if condition}}`.\n  - **Rich Input:** Full access to the payload as context.\n\n  **Use Cases:**\n  - Creating a personalized email body: \"Hello {{user.name}}...\"\n  - Constructing a complex file path: \"/archives/{{year}}/{{month}}/report.pdf\"",
  "capabilities": [
    "templating",
    "handlebars",
    "text-generation"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "template"
    ],
    "properties": {
      "template": {
        "type": "string",
        "title": "Template String",
        "format": "textarea",
        "description": "The text template using Handlebars syntax."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "payload": {
        "type": "object",
        "description": "Data context available in the template."
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "string",
        "description": "The rendered string."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Rendered",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "handlebars",
    "html",
    "template",
    "text"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "template"
  ],
  "properties": {
    "template": {
      "type": "string",
      "title": "Template String",
      "format": "textarea",
      "description": "The text template using Handlebars syntax."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "payload": {
      "type": "object",
      "description": "Data context available in the template."
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "string",
      "description": "The rendered string."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Rendered",
    "is_default": true
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
