Documentation
¶
Overview ¶
Package template provides a reusable Go text-template renderer and exposes it to Flow as the template.render action.
Typical use:
template.render(template: "Hello {{.name}}", variables: { name: "Flow" })
An embedded template is opened through the host's configured file resolver:
@embed welcome "templates/welcome.tmpl"
template.render(template: embed.welcome, variables: { name: "Flow" })
To load runtime template files, pass each fs.read Content byte slice as the source. Since fs.read is a stream, collect it and call a small pipeline per file:
@pipeline render_template_file
template.render(template_bytes: .Content, variables: { name: "Flow" })
@end
fs.walk(dir: "templates", ext: "tmpl") -> fs.read -> collect ->
{ files: . } -> distribute.map(action: pipeline.render_template_file, items: .files)
Package template implements text rendering for the template extension.
It uses Go's standard text/template syntax and never executes rendered text as a command. The renderer returns plain text; it does not HTML-escape output.
Index ¶
Examples ¶
Constants ¶
const ID = "template"
Variables ¶
var ( // ErrParse identifies invalid template syntax or an unknown template function. ErrParse = errors.New("template parse error") // ErrExecute identifies a failure while evaluating a parsed template, // including a reference to a missing map key. ErrExecute = errors.New("template execution error") )
var RenderAction = action.New("template.render", func(ctx context.Context, req RenderRequest) (string, error) { source, err := templateSource(ctx, req.Template, req.TemplateBytes) if err != nil { return "", err } result, err := Render(source, req.Variables) if err != nil { return "", xerr.BadRequest(err.Error()) } return result, nil }).Description("Render a text template with supplied variables"). Example(RenderRequest{ Template: "Hello {{.name}}", Variables: map[string]any{"name": "Flow"}, }).Build()
RenderAction exposes the package-agnostic renderer to Flow workflows.
Functions ¶
func Render ¶ added in v0.23.0
Render evaluates source with variables as the template's root data.
Templates use Go text/template syntax, for example `Hello {{.name}}`. A missing map key is an error (rather than silently rendering an empty value). Extra keys in variables are allowed and ignored when unused. A nil variables map behaves like an empty map. On any error, Render returns an empty string, never partial output.
Values are inserted as text and are not parsed again as template source. Render does not execute shell commands and does not HTML-escape its result.
Example ¶
got, err := Render("Hello {{.name}} — {{.literal}}", map[string]any{
"name": "srcpack",
"literal": "$(this remains text)",
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(got)
Output: Hello srcpack — $(this remains text)
Types ¶
type RenderRequest ¶
type RenderRequest struct {
// Template is an inline string or an embedded xfile.Ref. Set exactly one
// of Template and TemplateBytes.
Template any `json:"template,omitempty" cli:"template"`
// TemplateBytes is the source content, such as fs.read's .Content bytes.
TemplateBytes []byte `json:"template_bytes,omitempty" cli:"template_bytes"`
Variables map[string]any `json:"variables" cli:"variables"`
}
RenderRequest is the input shape for the template.render Flow action.