Useful template functions for Go templates. http://masterminds.github.io/sprig/
  • Go 99.9%
  • Makefile 0.1%
Find a file
Matt Farina 989da45d7c
Merge pull request #447 from zachburg/semver_test_coverage
Improve test coverage for `semver.go`
History 2025-07-18 09:29:21 -04:00
.github Updating dependencies 2025-07-17 15:16:47 -04:00
docs add SHA support to htpasswd 2024-10-10 10:27:26 +02:00
.gitignore Ignore additional glide-generated folder 2017-01-29 18:26:47 -08:00
CHANGELOG.md Updating the changelog for the 3.3.0 release 2024-08-29 14:58:25 -04:00
crypto.go add SHA support to htpasswd 2024-10-10 10:27:26 +02:00
crypto_test.go add SHA support to htpasswd 2024-10-10 10:27:26 +02:00
date.go Add duration filter (#224) 2020-01-23 21:07:04 -07:00
date_test.go Improve test coverage for date.go 2025-07-09 17:22:19 -07:00
defaults.go add all/any for mutiple condition 2020-12-04 02:42:40 +08:00
defaults_test.go add all/any for mutiple condition 2020-12-04 02:42:40 +08:00
dict.go Updating location of mergo 2024-08-28 11:10:50 -04:00
dict_test.go Add dig for dicts 2020-06-30 09:28:38 -07:00
doc.go Update doc.go 2023-05-17 15:55:40 -04:00
example_test.go Move example into its own file. 2016-03-30 15:37:21 -06:00
flow_control_test.go Add fail function 2017-05-16 14:25:22 -04:00
functions.go Merge pull request #400 from itzik-elayev/master 2024-08-29 09:41:35 -04:00
functions_linux_test.go Replaced functions name prefix: fp, os 2020-12-02 05:28:25 -03:00
functions_test.go Merge pull request #257 from rheaton/master 2020-12-02 10:22:49 -05:00
functions_windows_test.go Replaced functions name prefix: fp, os 2020-12-02 05:28:25 -03:00
go.mod Updating dependencies 2025-07-17 15:16:47 -04:00
go.sum Updating dependencies 2025-07-17 15:16:47 -04:00
issue_188_test.go Documenting and testing for deep copying with merging of dicts 2019-10-01 16:58:26 -04:00
LICENSE.txt Updating license for GitHub display 2020-04-16 10:42:55 -04:00
list.go Add chunk function to split array into smaller 2020-12-04 10:10:54 +07:00
list_test.go Add chunk function to split array into smaller 2020-12-04 10:10:54 +07:00
Makefile Moving dependency management to Go modules 2019-09-19 11:35:08 -04:00
network.go Add testing for Go 1.15 and drop testing for Go 1.12 2020-12-11 10:13:22 -05:00
network_test.go remove error handling from network functions, fix doc 2019-09-23 13:06:41 +02:00
numeric.go Add float64 arithmetic 2020-12-04 10:27:45 -05:00
numeric_test.go Add float64 arithmetic 2020-12-04 10:27:45 -05:00
README.md update version to v3 2022-04-04 20:20:43 +08:00
reflect.go Refactor into multiple files. 2017-03-13 14:56:11 -06:00
reflect_test.go lint 2018-12-09 00:43:45 +03:00
regex.go Add support for regex.QuoteMeta string conversion 2020-08-04 17:14:27 -06:00
regex_test.go Add support for regex.QuoteMeta string conversion 2020-08-04 17:14:27 -06:00
semver.go Moving dependency management to Go modules 2019-09-19 11:35:08 -04:00
semver_test.go Improve test coverage for semver.go 2025-07-10 13:26:59 -07:00
strings.go Fixing issues identified by gofmt, golint, and spell checks 2019-10-02 14:59:02 -04:00
strings_test.go Add test case for fmt.Stringer case of strval function. 2025-07-14 11:59:24 -07:00
url.go Fixing issues identified by gofmt, golint, and spell checks 2019-10-02 14:59:02 -04:00
url_test.go Fixing issues identified by gofmt, golint, and spell checks 2019-10-02 14:59:02 -04:00

Sprig: Template functions for Go templates

GoDoc Go Report Card Stability: Sustained

The Go language comes with a built-in template language, but not very many template functions. Sprig is a library that provides more than 100 commonly used template functions.

It is inspired by the template functions found in Twig and in various JavaScript libraries, such as underscore.js.

IMPORTANT NOTES

Sprig leverages mergo to handle merges. In its v0.3.9 release, there was a behavior change that impacts merging template functions in sprig. It is currently recommended to use v0.3.10 or later of that package. Using v0.3.9 will cause sprig tests to fail.

Package Versions

There are two active major versions of the sprig package.

  • v3 is currently stable release series on the master branch. The Go API should remain compatible with v2, the current stable version. Behavior change behind some functions is the reason for the new major version.
  • v2 is the previous stable release series. It has been more than three years since the initial release of v2. You can read the documentation and see the code on the release-2 branch. Bug fixes to this major version will continue for some time.

Usage

Template developers: Please use Sprig's function documentation for detailed instructions and code snippets for the >100 template functions available.

Go developers: If you'd like to include Sprig as a library in your program, our API documentation is available at GoDoc.org.

For standard usage, read on.

Load the Sprig library

To load the Sprig FuncMap:


import (
  "github.com/Masterminds/sprig/v3"
  "html/template"
)

// This example illustrates that the FuncMap *must* be set before the
// templates themselves are loaded.
tpl := template.Must(
  template.New("base").Funcs(sprig.FuncMap()).ParseGlob("*.html")
)


Calling the functions inside of templates

By convention, all functions are lowercase. This seems to follow the Go idiom for template functions (as opposed to template methods, which are TitleCase). For example, this:

{{ "hello!" | upper | repeat 5 }}

produces this:

HELLO!HELLO!HELLO!HELLO!HELLO!

Principles Driving Our Function Selection

We followed these principles to decide which functions to add and how to implement them:

  • Use template functions to build layout. The following types of operations are within the domain of template functions:
    • Formatting
    • Layout
    • Simple type conversions
    • Utilities that assist in handling common formatting and layout needs (e.g. arithmetic)
  • Template functions should not return errors unless there is no way to print a sensible value. For example, converting a string to an integer should not produce an error if conversion fails. Instead, it should display a default value.
  • Simple math is necessary for grid layouts, pagers, and so on. Complex math (anything other than arithmetic) should be done outside of templates.
  • Template functions only deal with the data passed into them. They never retrieve data from a source.
  • Finally, do not override core Go template functions.