text/template
Here is the translation into English:
text/template
component: Parses templates using text/template (opens new window). Used for message format transformation and format adaptation scenarios.
# Configuration
Field | Type | Description | Default Value |
---|---|---|---|
template | string | Template content or file path, use file: prefix for file paths | None |
template:
- Access message ID through the
.id
variable - Access message timestamp through the
.ts
variable - Access the original content of the message through the
.data
variable - Access the transformed data of the message through the
.msg
variable; if the message's dataType is JSON, you can access the fields ofmsg
using.msg.XX
- Access message metadata through the
.metadata
variable. For example,.metadata.customerName
- Access message type through the
.type
variable - Access data type through the
.dataType
variable
Example:
id:{{ .id }}
ts:{{ .ts }}
type:{{ .type }}
data:{{ .data }}
msg.name:{{ .msg.name }}
dataType:{{ .dataType }}
productType:{{ .metadata.productType }}
1
2
3
4
5
6
7
2
3
4
5
6
7
Here's the translation into English:
# Custom Function
You can register a custom function through github.com/rulego/rulego/builtin/funcs.TemplateFuncMap
, for example:
import "github.com/rulego/rulego/builtin/funcs"
funcs.TemplateFuncMap["escape"] = func(s string) string {
return strings.Replace(s, "\"", "\\\"", -1)
}
1
2
3
4
2
3
4
Usage:
"template": "data:\"{{.data | escape}}\""
1
# Relation Type
- Success: If executed successfully, send the message to the
Success
chain - Failure: If execution fails, send the message to the
Failure
chain
# Execution Result
Replace the msg
content with the template execution result.
# Configuration Example
{
"id": "s1",
"type": "text/template",
"name": "Template Transformation",
"configuration": {
"template": "type:{{ .type }}"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Edit this page on GitHub (opens new window)
Last Updated: 2024/10/23, 10:13:01