Documentation
¶
Overview ¶
Package templating provides an `Engine` for managing templates stored in an embedded filesystem. Use `Parse` to load multiple templates (via glob patterns), and `Render` to execute them with custom data.
Templates are HTML: data is escaped for the context it lands in, so a value carrying markup is rendered as text. Pass template.HTML to opt a value out, and only for markup you produced yourself.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Engine ¶ added in v0.1.23
type Engine struct {
// contains filtered or unexported fields
}
Engine is a simple wrapper around the Go templating engine.
Example ¶
package main
import (
"bytes"
"fmt"
"testing/fstest"
"github.com/andygeiss/cloud-native-utils/templating"
)
func main() {
fsys := fstest.MapFS{
"page.tmpl": &fstest.MapFile{Data: []byte(`{{ define "page" }}Hello {{ .Name }}{{ end }}`)},
}
engine := templating.NewEngine(fsys)
engine.Parse("*.tmpl")
// Data is escaped for the context it lands in.
var out bytes.Buffer
_ = engine.Render(&out, "page", struct{ Name string }{Name: "<b>Alice</b>"})
fmt.Println(out.String())
}
Output: Hello <b>Alice</b>
Click to show internal directories.
Click to hide internal directories.