Skip to content
On this page

templatex

go
import "github.com/kyoto-framework/zen/v3/templatex"

is a package that helps with templating. It provides a way to use structs as funcmap extensions and provides a set of extensions based on zen library.

You have at least 2 ways of using templatex: using prepared funcmap, or using extensions separately.

To use prepared funcmap, just use predefined FuncMap:

go
// Use templatex.FuncMap as the only source of functions
funcmap := templatex.FuncMap
// Combine with your own functions
funcmap := mapx.Merge(
	templatex.FuncMap,
	ownfuncmap,
	...
)

To use extensions separately, use import function and provide extension instance:

go
funcmap := template.FuncMap{
	"jsonx": templatex.Import(&templatex.JsonExtension{}),
}

In the template, you'll be able to use functions from the extension:

{{ jsonx.String .Value }}

Each extension also provides own funcmap if you want to use methods as top-level, instead of using dot notation:

go
funcmap := mapx.Merge(
	templatex.Import(&ConvExtension{}).FuncMap(), // This will add "bool", "float", etc.
)

In the template, you'll be able to use functions in this way:

{{ bool .Value }}

Please, use index to explore available extensions.