Debian Codegraph @

README.md

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