sprig

repository·master·Indexed 26 days ago

https://github.com/masterminds/sprig

A 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.

Tokens
15.9K
Snippets
59
Records
169
Agent score
86%

What's inside sprig

  1. Overview of Sprig template functions

    master
    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.
  2. Install and load the Sprig library in Go

    master

    To use Sprig's template functions in your Go application, import github.com/Masterminds/sprig/v3 and pass sprig.FuncMap() to your template's Funcs method.

    Important: The FuncMap must be set on the template instance before the templates are parsed (e.g., via Parse or ParseGlob).

    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")
    )
  3. Handle prerelease versions in SemVer comparisons

    master

    Prereleases (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.3 will not match 1.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-Z comes before a-z).
  4. Use Sprig template functions in templates

    master

    Sprig 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!
  5. Create and manipulate Sprig lists

    master

    Sprig provides a list type 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 the list function.

    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 5
  6. Inject Sprig functions into Go templates

    master

    To use Sprig's library of template functions in your Go application, use the sprig.FuncMap() method. This returns a template.FuncMap that 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())
  7. Integrate Sprig functions into Go templates

    master

    To use Sprig's utility functions within Go html/template or text/template files, you must register the Sprig function map using the Funcs() 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())
  8. Extract multiple values with pluck

    master

    The pluck function takes a single key and multiple dictionaries, returning a list of 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