RuleGo RuleGo
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • StreamSQL
  • AI Agent Framework
  • TPCLAW Agent Platform (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • StreamSQL
  • AI Agent Framework
  • TPCLAW Agent Platform (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • Quick Start

  • Rule Chain

  • Standard Components

    • Standard Components Overview
    • common

    • filter

    • action

    • transform

      • jsTransform
      • exprTransform
      • metadataTransform
      • text/template
        • Configuration
        • Configuration example
        • Custom Functions
          • Registration Timing
          • Function Signatures & Naming
    • external

    • flow

  • Extension Components

  • Custom Components

  • Components marketplace

  • Visualization

  • AOP

  • Trigger

  • Advanced Topic

  • Agent Framework

  • RuleGo-Server

  • FAQ

  • Endpoint Module

  • Support

  • StreamSQL

目录

text/template

text/template component: Parse templates using text/template (opens new window). It is used in scenarios such as message format transformation, content templating, and data adaptation. It supports a rich set of template syntax and custom function extensions.

# Configuration

Field Type Description Default Value
template string Template content or template file path. Use the prefix file: to indicate a file path, such as file:/path/to/tpl.txt None

Supported template variables:

  • .id - Message ID
  • .ts - Message timestamp (in milliseconds)
  • .data - Raw message content
  • .msg - Message body object (when of JSON type, you can access fields with .msg.field)
  • .metadata - Message metadata object
  • .msgType - Message type
  • .dataType - Data type

Template example:

 {{ .msg.name }} - Retrieve the name field from the message
 {{ .metadata.deviceType }} - Retrieve the device type from metadata
 {{ .msgType }} - Retrieve the message type
 {{ .ts }} - Retrieve the message timestamp
 {{ if gt .msg.temperature 30 }}High Temperature{{ else }}Normal{{ end }} - Conditional judgment
 {{ range .msg.items }}{{ .name }},{{ end }} - Iterate over an array
 {{ printf "%.2f" .msg.value }} - Format a number
 {{ .msg.name | upper }} - Convert to uppercase
 {{ .msg.content | replace "old" "new" }} - Replace text
1
2
3
4
5
6
7
8
9

# Configuration example

{
  "id": "s1",
  "type": "text/template",
  "name": "Template Transformation",
  "configuration": {
    "template": "type:{{ .type}}"
  }
}
1
2
3
4
5
6
7
8

# Custom Functions

The component is built on the Go standard library text/template. At node initialization it loads all registered functions from the global function table funcs.TemplateFunc (opens new window) and binds them to the template engine. Upper-layer applications can register custom functions via the github.com/rulego/rulego/builtin/funcs package and use them in templates just like built-in functions.

Register functions:

import (
	"strings"
	"text/template"

	"github.com/rulego/rulego/builtin/funcs"
)

// Register a single function
funcs.TemplateFunc.Register("upper", strings.ToUpper)

// Register multiple functions
funcs.TemplateFunc.RegisterAll(template.FuncMap{
	"upper":   strings.ToUpper,
	"replace": strings.ReplaceAll,
})

// Unregister / lookup
funcs.TemplateFunc.UnRegister("upper")
f, ok := funcs.TemplateFunc.Get("upper")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Use in templates:

{{ upper .msg.name }}                    {{/* Direct call */}}
{{ .msg.name | upper }}                  {{/* Pipeline call */}}
{{ replace .msg.content "old" "new" }}   {{/* Multiple arguments */}}
1
2
3

Built-in functions: Only escape is registered by default (it escapes backslashes, double quotes, newlines, and other JSON special characters). The upper, replace, etc. appearing in the template examples above must be registered yourself.

# Registration Timing

The text/template function map (FuncMap) must be bound before the template is parsed, therefore:

  • ✅ Register before rulego.New(...) creates the rule chain — takes effect for all text/template nodes.
  • ❌ Registering after a node has been initialized takes no effect; you must reload the rule chain so the node re-runs Init to refresh the function snapshot.

# Function Signatures & Naming

  • Return 1 value, or 2 values (the second must be of type error); when error is returned, an execution failure is propagated to the Failure branch.
  • The function name must be a valid Go identifier; the first letter can be either upper- or lowercase (the built-in escape is lowercase).
  • TemplateFunc is a process-wide global registry; registered functions are shared by all text/template nodes. Per-node function sets are not supported out of the box.
Edit this page on GitHub (opens new window)
Last Updated: 2026/07/16, 02:02:13
metadataTransform
restApiCall

← metadataTransform restApiCall→

Theme by Vdoing | Copyright © 2023-2026 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式