sprig

package
v2.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2025 License: Apache-2.0, MIT Imports: 25 Imported by: 0

Documentation

Overview

Package sprig provides template functions for Go.

This package contains a number of utility functions for working with data inside of Go `html/template` and `text/template` files.

To add these functions, use the `template.Funcs()` method:

t := template.New("foo").Funcs(sprig.FuncMap())

Note that you should add the function map before you parse any template files.

In several cases, Sprig reverses the order of arguments from the way they
appear in the standard library. This is to make it easier to pipe
arguments into functions.

See http://masterminds.github.io/sprig/ for more detailed documentation on each of the available functions.

Example
// Set up variables and template.
vars := map[string]any{"Name": "  John Jacob Jingleheimer Schmidt "}
tpl := `Hello {{.Name | trim | lower}}`

// Get the Sprig function map.
fmap := TxtFuncMap()
t := template.Must(template.New("test").Funcs(fmap).Parse(tpl))

err := t.Execute(os.Stdout, vars)
if err != nil {
	fmt.Printf("Error during template execution: %s", err)
	return
}
Output:

Hello john jacob jingleheimer schmidt

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func TxtFuncMap

func TxtFuncMap() template.FuncMap

TxtFuncMap produces the function map.

Use this to pass the functions into the template engine:

tpl := template.New("foo").Funcs(sprig.FuncMap()))

TxtFuncMap returns a 'text/template'.FuncMap

Types

This section is empty.