wg-quicker/vendor/github.com/Masterminds/sprig
2022-01-07 20:20:48 +01:00
..
.gitignore build: managing tools in go modules 2022-01-07 20:20:48 +01:00
.travis.yml build: managing tools in go modules 2022-01-07 20:20:48 +01:00
appveyor.yml build: managing tools in go modules 2022-01-07 20:20:48 +01:00
CHANGELOG.md build: managing tools in go modules 2022-01-07 20:20:48 +01:00
crypto.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
date.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
defaults.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
dict.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
doc.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
functions.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
glide.yaml build: managing tools in go modules 2022-01-07 20:20:48 +01:00
LICENSE.txt build: managing tools in go modules 2022-01-07 20:20:48 +01:00
list.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
Makefile build: managing tools in go modules 2022-01-07 20:20:48 +01:00
network.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
numeric.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
README.md build: managing tools in go modules 2022-01-07 20:20:48 +01:00
reflect.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
regex.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
semver.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
strings.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00
url.go build: managing tools in go modules 2022-01-07 20:20:48 +01:00

Sprig: Template functions for Go templates

Stability: Sustained Build Status

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.

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