Flanksource Gomplate is a fork of hairyhenderson/gomplate that provides a unified templating library supporting multiple expression languages.
- Go Text/Template – Full Go
text/templatesupport with an extended function library from gomplate (base64, collections, crypto, data formats, filepath, math, random, regexp, strings, time, and more) - CEL (Common Expression Language) – CEL support with:
- Standard CEL operators and built-ins
- cel-go extensions (strings, encoders, lists, math, sets)
- Kubernetes-specific helpers (
k8s.*); health, status, neat and topology are opt-in - AWS helpers (
aws.*) - Many gomplate functions remapped into CEL (
base64,math,random,regexp,filepath,crypto,sets, etc.) - Optional-type support (
obj.?field.orValue("default"))
- JavaScript (otto) – Lightweight JS evaluation via otto, opt-in
- Struct templating – Walk Go structs/maps and apply templates to string fields in-place
The Kubernetes health, status, neat and topology functions and JavaScript templates pull in
Kubernetes, Lua and otto dependencies, so the core package does not import them. A program
that needs them imports github.com/flanksource/gomplate/v4/kubernetes and
github.com/flanksource/gomplate/v4/js and registers them once at startup with
gomplate.Register(kubernetes.Extension(), js.Extension()).
Without kubernetes.Extension(), CEL expressions such as k8s.isHealthy(pod) fail with an
undeclared reference and Go templates fail to parse isHealthy. Without js.Extension(),
Template.Javascript returns an error. k8s.cpuAsMillicores, k8s.memoryAsBytes and the
list, URL and regex functions stay in the core.
A program that embeds gomplate describes its expression surface once, with github.com/flanksource/gomplate/v4/catalog, and derives from it everything an author sees: the completion and hover catalogue its editor or playground loads, the Markdown reference pages, and the Monaco tokenizers a static site highlights with. The catalogue holds gomplate's core, the extensions registered with gomplate.Register, and the program's own functions; sections and functions of an opt-in group the program does not register are left out. Build reads no Go source, so a deployed binary can serve it.
c, err := catalog.Build(catalog.Host{
Name: "Acme", // names the program in the generated text
// The same declarations the program evaluates with, documented with
// cel.FunctionDocs / cel.OverloadExamples.
CEL: acmeDeclarations,
// The program's template FuncMap; every function needs a description.
Template: must(catalog.TemplateFunctions(acmeFuncs, acmeDocs)),
Docs: catalog.Docs{
CEL: catalog.Page{URL: "/docs/reference/cel/", Options: gendocs.Options{
Frontmatter: true,
Intro: "Acme evaluates CEL in rule conditions.",
Append: []gendocs.Appendix{{Title: "Acme functions", Markdown: "### acme.lookup\n\n…"}},
}},
GoTemplate: catalog.Page{URL: "/docs/reference/templates/"},
},
})
// c.Spec -> serve as JSON to the playground (complete; each function's docsUrl links its section)
// c.CELPage.Markdown, c.GoTemplatePage.Markdown -> publish at the URLs above
// c.Languages() -> Monaco languages.json for a site's code highlightingAn editor loading the served spec (mergeSpec, registerGomplateLanguages) replaces its packaged catalogue with it because the spec is marked complete, and hover and completion link each function to its reference section. The pages tag untagged code fences cel or gomplate, the language ids of those tokenizers. gendocs renders the pages on its own for a program that only wants Markdown.
| Language | Example | Documentation |
|---|---|---|
| Go Template | {{ .name | strings.ToUpper }} |
GO_TEMPLATE.md |
| CEL | name.upperAscii() |
CEL.md |
# Arithmetic
2 + 3 // 5
"hello" + " world" // "hello world"
[1,2] + [3,4] // [1,2,3,4]
# Comparison / Logic
x > 5 && y != null
true ? "yes" : "no"
# String methods
"hello world".upperAscii() // "HELLO WORLD"
"hello world".split(" ") // ["hello", "world"]
"hello".startsWith("he") // true
"hello world".contains("world") // true
# Collections
[1,2,3].filter(e, e > 1) // [2, 3]
[1,2,3].map(e, e * 2) // [2, 4, 6]
[1,2,3].all(e, e > 0) // true
{"a":1,"b":2}.keys() // ["a","b"]
# Optional / null-safety
obj.?field.orValue("default")
obj.?a.?b.orValue("fallback")
# Kubernetes (k8s.isHealthy needs kubernetes.Extension())
k8s.isHealthy(pod)
k8s.cpuAsMillicores("500m") // 500
k8s.memoryAsBytes("1Gi")
# Math
math.Add([1,2,3]) // 6
math.greatest([1,2,3]) // 3
# Time
time.Now()
time.Since(timestamp("2023-01-01T00:00:00Z"))
See CEL.md for full reference.
# Variables and output
{{ .name }}
{{ $x := "hello" }}{{ $x | strings.ToUpper }}
# Control flow
{{ if eq .status "ok" }}OK{{ else }}FAIL{{ end }}
{{ range .items }}{{ . }}{{ end }}
# String functions
{{ "hello world" | strings.ToUpper }} // HELLO WORLD
{{ "hello world" | strings.Split " " }} // [hello world]
{{ "hello" | strings.Repeat 3 }} // hellohellohello
# Data
{{ '{"key":"val"}' | json }} // map access
{{ .obj | toJSON }}
{{ .obj | toYAML }}
# Collections
{{ coll.Dict "a" 1 "b" 2 | toJSON }} // {"a":1,"b":2}
{{ slice 1 2 3 | coll.Reverse | toJSON }} // [3,2,1]
{{ coll.Sort (slice "b" "a" "c") | toJSON }} // ["a","b","c"]
# Math
{{ math.Add 1 2 3 }} // 6
{{ math.Max 1 5 3 }} // 5
# base64
{{ base64.Encode "hello" }} // aGVsbG8=
{{ "aGVsbG8=" | base64.Decode }} // hello
# Crypto
{{ crypto.SHA256 "hello" }}
# Delimiters (change if conflict with Helm)
# gotemplate: left-delim=$[[ right-delim=]]
$[[ .name ]]
See GO_TEMPLATE.md for full reference.
import "github.com/flanksource/gomplate/v4"result, err := gomplate.RunTemplate(map[string]any{
"name": "world",
}, gomplate.Template{
Template: `Hello, {{ .name }}!`,
})result, err := gomplate.RunTemplate(map[string]any{
"name": "world",
}, gomplate.Template{
Expression: `"Hello, " + name + "!"`,
})type Config struct {
Message string
Count int
}
cfg := Config{Message: "Hello, {{ .name }}!"}
err := gomplate.Walk(map[string]any{"name": "world"}, &cfg)
// cfg.Message == "Hello, world!"make build creates bin/ceval with the playground UI and Go WASM evaluator embedded. Run ./bin/ceval serve and open http://127.0.0.1:8321; select Server or Browser (WASM) evaluation. See the playground guide for development, hosting and binary releases.
A small helper command is available at cmd/ceval to evaluate CEL expressions against an input YAML or JSON file.
Run it with:
go run ./cmd/ceval -f env.yaml -e "labels['a/b/c']"Example input file:
labels:
a/b/c: dOutput:
d