本页内容
Sommatic 文档
打开控制台

Render Template

使用 Handlebars 模板引擎生成动态文本。

以 Markdown 查看
在以下应用中打开
数据 可用 data.template 版本 1.0.0 执行方式: sync

使用输入 payload 渲染 Handlebars 模板,并将结果文本以 text 添加到 payload。

该算子将数据变量合并到文本模板中,非常适合构建邮件、短信、HTML 文档或 SQL 查询。

功能特性:

  • Handlebars 语法: 使用 {{variable}}、{{#each list}}、{{#if condition}}。
  • 丰富的输入: 可将整个载荷作为上下文完整访问。

适用场景:

  • 生成个性化邮件正文:“Hello {{user.name}}...”
  • 构建复杂的文件路径:“/archives/{{year}}/{{month}}/report.pdf”

工作原理

  1. 从配置中原样读取 template:它不经过 Expression Engine,因此其中所有 {{ }} 都属于 Handlebars。其他配置键照常解析。
  2. 用 Handlebars 4 编译,并以 payload 为上下文渲染:{{customer.name}}、{{#each items}}...{{/each}}、{{#if paid}}...{{/if}}。
  3. 通过 out 返回 payload 加上 text。

在工作流中

节点在工作流文件(draft_nodes[])中的写法,以及一次成功运行的 envelope。

{
  "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."
  }
}

配置

template string 必填

使用 Handlebars 语法的文本模板。

配置中会显示通用执行设置,但该节点目前不执行重试(retryOnFail、maxTries)。所有节点均未实现 continueOnFail。

代码中的行为

除 schema 描述之外,实现代码对这些键的实际处理。

template
只能访问 payload 字段。Expression Engine 变量($json、$("Node")、$now、$execution)在此不可用:请写 {{order_id}},而不是 {{ $json.order_id }}。空模板渲染为 ""。

输出端口

out 默认

模板渲染完成。

数据
{
  "customer": {
    "name": "Ana"
  },
  "order_id": "ord_123",
  "text": "Hello Ana, your order ord_123 is ready."
}
error

模板存在语法错误。

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

在表达式中

后续节点可通过 $("Render Template").item.json.<路径> 读取此节点输出中的内容(请把 Render Template 换成你的节点标签)。Editor 标记工作流编辑器在 Previous Nodes 中提示的路径(输出契约);Worker 标记 worker 实际通过 out 返回的路径,依据“端口”一节所示的输出。

表达式EditorWorker
$("Render Template").item.json.resultstringworker 不返回:读取结果为 undefined渲染后的字符串。 —
$("Render Template").item.json.customerobject编辑器不提示= {…}—
$("Render Template").item.json.customer.namestring编辑器不提示= "Ana"—
$("Render Template").item.json.order_idstring编辑器不提示= "ord_123"—
$("Render Template").item.json.textstring编辑器不提示= "Hello Ana, your order ord_123 is ready."—

— Editor: 编辑器会提示它,但 worker 并不返回:读取结果为 undefined。

— Worker: worker 会返回它,但编辑器不会提示:请手动输入。对象内部的键可能取决于你的数据或配置(例如每个标签一个键)。

错误

消息 端口 原因与处理
Template Error: Parse error on line <n>: <details> error Handlebars 语法无效,例如 {{#if}} 块未闭合。

输入契约

payload object 可选

模板中可用的数据上下文。

输出契约

result string 可选

渲染后的字符串。

契约(JSON Schema)

与编排器声明完全一致的定义。All 是完整的 *.definition.js;可与上方各节对照,找出文档与 schema 之间的差异。发布前可用它校验节点。

{
  "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
    }
  }
}

执行

执行方式
sync
有状态
否
重试(retryOnFail)
否
能力
templatinghandlebarstext-generation
标签
handlebarshtmltemplatetext

说明与限制

  • {{ }} 会转义 HTML 字符(&、<、>、引号);需要原样输出时请用 {{{ }}}。
  • 只提供 Handlebars 内置 helper(if、unless、each、with、lookup、log);未注册自定义 helper。缺失字段渲染为空文本。
  • 输出键为 text(输出契约写的是 result);已有的 text 键会被覆盖。
  • 当返回的信封没有 data(或 data 为假值)时,引擎会把整个信封作为节点输出:经 error 端口时,下一个节点收到的 payload 是 { ok: false, errors: [...], port: "error" }。

已对照编排器代码核实 · 31e874b · 2026-11-26

安装智能体工具

为你的编程智能体接入 Sommatic AI 官方文档:技能包、文档 MCP 服务器与 Markdown 页面,无需再抓取网页。

仅安装技能包,不会自动更新;需要时重新运行即可。

npx skills add https://docs.sommatic.ai
智能体如何读取本文档