• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H16-Apr-2020-3520

docs/H16-Apr-2020-1,8651,201

.gitignoreH A D16-Apr-202016 32

CHANGELOG.mdH A D16-Apr-202011.4 KiB335213

MakefileH A D16-Apr-2020179 108

README.mdH A D16-Apr-20203.9 KiB10274

crypto.goH A D16-Apr-202012.4 KiB510436

crypto_test.goH A D16-Apr-20207.5 KiB305265

date.goH A D16-Apr-20203 KiB153128

date_test.goH A D16-Apr-20203.3 KiB12194

defaults.goH A D16-Apr-20202.9 KiB12390

defaults_test.goH A D16-Apr-20203.4 KiB140122

dict.goH A D16-Apr-20202.9 KiB149125

dict_test.goH A D16-Apr-20207.4 KiB296273

doc.goH A D16-Apr-2020671 201

example_test.goH A D16-Apr-2020526 2617

flow_control_test.goH A D16-Apr-2020279 1713

functions.goH A D16-Apr-20209.2 KiB340258

functions_test.goH A D16-Apr-20203.4 KiB11894

go.modH A D16-Apr-2020405 1613

go.sumH A D16-Apr-20204.9 KiB5756

issue_188_test.goH A D16-Apr-20201.2 KiB3421

list.goH A D16-Apr-20208 KiB420338

list_test.goH A D16-Apr-202011.7 KiB339307

network.goH A D16-Apr-2020214 139

network_test.goH A D16-Apr-2020286 1913

numeric.goH A D16-Apr-20202.5 KiB156134

numeric_test.goH A D16-Apr-20205.4 KiB243226

reflect.goH A D16-Apr-2020548 2921

reflect_test.goH A D16-Apr-20201.4 KiB7462

regex.goH A D16-Apr-20201.8 KiB8066

regex_test.goH A D16-Apr-20205.3 KiB199172

semver.goH A D16-Apr-2020401 2418

semver_test.goH A D16-Apr-2020876 3227

strings.goH A D16-Apr-20204.5 KiB237198

strings_test.goH A D16-Apr-20207.4 KiB279240

url.goH A D16-Apr-20201.6 KiB6758

url_test.goH A D16-Apr-20202 KiB8877

README.md

1# Sprig: Template functions for Go templates
2
3[![GoDoc](https://img.shields.io/static/v1?label=godoc&message=reference&color=blue)](https://pkg.go.dev/github.com/Masterminds/sprig/v3)
4[![Go Report Card](https://goreportcard.com/badge/github.com/Masterminds/sprig)](https://goreportcard.com/report/github.com/Masterminds/sprig)
5[![Stability: Sustained](https://masterminds.github.io/stability/sustained.svg)](https://masterminds.github.io/stability/sustained.html)
6[![](https://github.com/Masterminds/sprig/workflows/Tests/badge.svg)](https://github.com/Masterminds/sprig/actions)
7
8The Go language comes with a [built-in template
9language](http://golang.org/pkg/text/template/), but not
10very many template functions. Sprig is a library that provides more than 100 commonly
11used template functions.
12
13It is inspired by the template functions found in
14[Twig](http://twig.sensiolabs.org/documentation) and in various
15JavaScript libraries, such as [underscore.js](http://underscorejs.org/).
16
17## IMPORTANT NOTES
18
19Sprig leverages [mergo](https://github.com/imdario/mergo) to handle merges. In
20its v0.3.9 release there was a behavior change that impacts merging template
21functions in sprig. It is currently recommended to use v0.3.8 of that package.
22Using v0.3.9 will cause sprig tests to fail. The issue in mergo is tracked at
23https://github.com/imdario/mergo/issues/139.
24
25## Package Versions
26
27There are two active major versions of the `sprig` package.
28
29* v3 is currently stable release series on the `master` branch. The Go API should
30  remain compatible with v2, the current stable version. Behavior change behind
31  some functions is the reason for the new major version.
32* v2 is the previous stable release series. It has been more than three years since
33  the initial release of v2. You can read the documentation and see the code
34  on the [release-2](https://github.com/Masterminds/sprig/tree/release-2) branch.
35  Bug fixes to this major version will continue for some time.
36
37## Usage
38
39**Template developers**: Please use Sprig's [function documentation](http://masterminds.github.io/sprig/) for
40detailed instructions and code snippets for the >100 template functions available.
41
42**Go developers**: If you'd like to include Sprig as a library in your program,
43our API documentation is available [at GoDoc.org](http://godoc.org/github.com/Masterminds/sprig).
44
45For standard usage, read on.
46
47### Load the Sprig library
48
49To load the Sprig `FuncMap`:
50
51```go
52
53import (
54  "github.com/Masterminds/sprig"
55  "html/template"
56)
57
58// This example illustrates that the FuncMap *must* be set before the
59// templates themselves are loaded.
60tpl := template.Must(
61  template.New("base").Funcs(sprig.FuncMap()).ParseGlob("*.html")
62)
63
64
65```
66
67### Calling the functions inside of templates
68
69By convention, all functions are lowercase. This seems to follow the Go
70idiom for template functions (as opposed to template methods, which are
71TitleCase). For example, this:
72
73```
74{{ "hello!" | upper | repeat 5 }}
75```
76
77produces this:
78
79```
80HELLO!HELLO!HELLO!HELLO!HELLO!
81```
82
83## Principles Driving Our Function Selection
84
85We followed these principles to decide which functions to add and how to implement them:
86
87- Use template functions to build layout. The following
88  types of operations are within the domain of template functions:
89  - Formatting
90  - Layout
91  - Simple type conversions
92  - Utilities that assist in handling common formatting and layout needs (e.g. arithmetic)
93- Template functions should not return errors unless there is no way to print
94  a sensible value. For example, converting a string to an integer should not
95  produce an error if conversion fails. Instead, it should display a default
96  value.
97- Simple math is necessary for grid layouts, pagers, and so on. Complex math
98  (anything other than arithmetic) should be done outside of templates.
99- Template functions only deal with the data passed into them. They never retrieve
100  data from a source.
101- Finally, do not override core Go template functions.
102