Skip to content

flanksource/gomplate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2,004 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gomplate

Flanksource Gomplate is a fork of hairyhenderson/gomplate that provides a unified templating library supporting multiple expression languages.

Features

  • Go Text/Template – Full Go text/template support 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
    • celext extensions (strings, encoders, lists, math, sets)
    • Kubernetes-specific helpers (k8s.*)
    • 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
  • Struct templating – Walk Go structs/maps and apply templates to string fields in-place

Quick Reference

Language Example Documentation
Go Template {{ .name | strings.ToUpper }} GO_TEMPLATE.md
CEL name.upperAscii() CEL.md

CEL Quick Reference

# 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(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.

Go Template Quick 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.

Installation

import "github.com/flanksource/gomplate/v3"

Usage

Go Template

result, err := gomplate.RunTemplate(map[string]any{
    "name": "world",
}, gomplate.Template{
    Template: `Hello, {{ .name }}!`,
})

CEL

result, err := gomplate.RunTemplate(map[string]any{
    "name": "world",
}, gomplate.Template{
    Expression: `"Hello, " + name + "!"`,
})

Struct Templating

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!"

About

A flexible commandline tool for template rendering. Supports lots of local and remote datasources.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Go 95.2%
  • JavaScript 2.4%
  • Makefile 1.5%
  • Other 0.9%