sprig
repository·master·Indexed 26 days ago
https://github.com/masterminds/sprigA library providing over 100 commonly used template functions for Go's built-in text/template and html/template packages. It includes utilities for cryptography (SHA, bcrypt, AES, UUIDs), date and time formatting, JSON encoding/decoding, and dictionary manipulation (dict, get, set, merge), inspired by Twig and Underscore.js.
What's inside sprig
- Sprig is a library providing over 70 template functions designed for use with Go's template language. It extends the standard capabilities of Go templates with specialized functions for strings, math, dates, encoding, dictionaries, and more.
Install and load the Sprig library in Go
masterTo use Sprig's template functions in your Go application, import
github.com/Masterminds/sprig/v3and passsprig.FuncMap()to your template'sFuncsmethod.Important: The
FuncMapmust be set on the template instance before the templates are parsed (e.g., viaParseorParseGlob).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") )Handle prerelease versions in SemVer comparisons
masterPrereleases (e.g.,
1.2.3-beta.1) have lower precedence than their associated stable releases (e.g.,1.2.3).Important Behavior:
- By default, constraints without a prerelease comparator will skip prerelease versions. For example,
>=1.2.3will not match1.2.3-alpha. - To include prereleases in your evaluation, use a prerelease version in the constraint, such as
>=1.2.3-0. - Sorting follows ASCII order (e.g.,
A-Zcomes beforea-z).
- By default, constraints without a prerelease comparator will skip prerelease versions. For example,
Use Sprig template functions in templates
masterSprig provides over 100 template functions. By convention, all function names are lowercase. You can use them using standard Go template syntax, including piping functions together.
For a complete list of functions and detailed usage instructions, refer to the Sprig function documentation.
{{ "hello!" | upper | repeat 5 }}Output:
HELLO!HELLO!HELLO!HELLO!HELLO!Create and manipulate Sprig lists
masterSprig provides a
listtype for handling sequential data. Lists are designed to be immutable; functions that modify a list return a new list rather than altering the original. You can create a list using thelistfunction.Note: For Go developers, a list is implemented as a
[]interface{}. You can pass[]interface{}items into your template context and use all Sprig list functions on them.$myList := list 1 2 3 4 5Inject Sprig functions into Go templates
masterTo use Sprig's library of template functions in your Go application, use the
sprig.FuncMap()method. This returns atemplate.FuncMapthat can be passed directly into a new template instance using the.Funcs()method.By default,
sprig.FuncMap()returns the HTML-safe version of the function map.tpl := template.New("foo").Funcs(sprig.FuncMap())Integrate Sprig functions into Go templates
masterTo use Sprig's utility functions within Go
html/templateortext/templatefiles, you must register the Sprig function map using theFuncs()method on your template instance.Important: You must call
.Funcs(sprig.FuncMap())before you call.Parse()or.ParseFiles()on the template. If you parse the template before adding the functions, the template engine will fail to recognize the Sprig functions during parsing.t := template.New("foo").Funcs(sprig.FuncMap())Convert a collection to a list of strings with toStrings
masterUse the
toStringsfunction to take a list-like collection (list, slice, or array) and produce a slice where every element is a string.list 1 2 3 | toStringsConvert unix octal permissions to decimal with toDecimal
masterUse the
toDecimalfunction to convert a unix octal permission string into its decimalint64representation."0777" | toDecimalCompare complex values with deepEqual
masterThedeepEqualfunction returns true if two values are deeply equal. Unlike the built-ineqfunction,deepEqualworks correctly for non-primitive types like lists or maps.Convert seconds to a duration with `duration`
masterThe
durationfunction takes a string representing a number of seconds and converts it into atime.Durationobject.duration "95"Extract multiple values with pluck
masterThe
pluckfunction takes a single key and multiple dictionaries, returning alistof all matching values found.- If a key is not found in a dictionary, that dictionary is skipped (the resulting list length will be less than the number of input dictionaries).
- If a key is found but the value is empty, the empty value is still included in the list.
A common pattern to get the first match from a collection is
pluck "key" $dict1 $dict2 | first.pluck "name1" $myDict $myOtherDict