模板解析器
  text/template组件:使用 text/template (opens new window) 解析模板。用于消息格式转换、内容模板化、数据适配等场景。支持丰富的模板语法和自定义函数扩展。
# 配置
| 字段 | 类型 | 说明 | 默认值 | 
|---|---|---|---|
| template | string | 模板内容或模板文件路径。使用file:前缀表示文件路径,如file:/path/to/tpl.txt | 无 | 
支持的模板变量:
.id- 消息ID.ts- 消息时间戳(毫秒).data- 原始消息内容.msg- 消息体对象(JSON类型时可用.msg.field访问字段).metadata- 消息元数据对象.msgType- 消息类型.dataType- 数据类型
模板示例:
 {{ .msg.name }} - 获取消息中的name字段
 {{ .metadata.deviceType }} - 获取元数据中的设备类型
 {{ .msgType }} - 获取消息类型
 {{ .ts }} - 获取消息时间戳
 {{ if gt .msg.temperature 30 }}高温{{ else }}正常{{ end }} - 条件判断
 {{ range .msg.items }}{{ .name }},{{ end }} - 遍历数组
 {{ printf "%.2f" .msg.value }}` - 格式化数值
 {{ .msg.name | upper }} - 转换为大写
 {{ .msg.content | replace "old" "new" }} - 替换文本
 1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 配置示例
{
  "id": "s1",
  "type": "text/template",
  "name": "模板转换",
  "configuration": {
    "template": "type:{{ .type}}"
  }
}
 1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
在 GitHub 上编辑此页  (opens new window)
  上次更新: 2025/09/03, 10:09:04