Useful template functions for Go templates.
Go Makefile
Clone or download
technosophos Merge pull request #97 from snooc/fix-tls
Generate Serial Number for TLS certs
Latest commit 77bb58b Jul 25, 2018
Permalink
Failed to load latest commit information.
docs Merge pull request #102 from stannum-l/fix-doc Jul 25, 2018
.gitignore Ignore additional glide-generated folder Jan 30, 2017
.travis.yml Merge pull request #93 from mattfarina/appveyor Apr 3, 2018
CHANGELOG.md Updating changelog for 2.15 release Apr 3, 2018
LICENSE.txt Added License (#1). Nov 25, 2013
Makefile Only install glide during 'make setup' if not already installed. Mar 13, 2017
README.md Updated README with pointers to new docs. Apr 7, 2017
appveyor.yml Adding appveyor congif for windows testing Apr 2, 2018
crypto.go Generate serial number Apr 30, 2018
crypto_test.go Generate serial number Apr 30, 2018
date.go Fix how rounding occurs by using internal Round function for ago Apr 3, 2018
date_test.go Fix how rounding occurs by using internal Round function for ago Apr 3, 2018
defaults.go Minor improvement in code quality Jun 20, 2018
defaults_test.go Add ternary function Mar 28, 2018
dict.go Extract values of dict with `values` function Jun 25, 2018
dict_test.go Extract values of dict with `values` function Jun 25, 2018
doc.go Extract values of dict with `values` function Jun 25, 2018
example_test.go Move example into its own file. Mar 30, 2016
flow_control_test.go Add fail function May 16, 2017
functions.go Merge pull request #101 from gongdo/master Jul 25, 2018
functions_test.go Add 'snakecase', 'camelcase' and 'shuffle' May 2, 2017
glide.lock replace satori/go.uuid with google/uuid Apr 2, 2018
glide.yaml replace satori/go.uuid with google/uuid Apr 2, 2018
list.go Modify panic message for list.slice Jun 1, 2018
list_test.go Fix file permissions Jun 1, 2018
numeric.go Minor improvement in code quality Jun 20, 2018
numeric_test.go Minor improvement in code quality Jun 20, 2018
reflect.go Refactor into multiple files. Mar 13, 2017
reflect_test.go Refactor into multiple files. Mar 13, 2017
regex.go Minor improvement in code quality Jun 20, 2018
regex_test.go Minor improvement in code quality Jun 20, 2018
semver.go Add semver and semverCompare. Mar 15, 2017
semver_test.go Add semver and semverCompare. Mar 15, 2017
strings.go add splitn function Jul 11, 2018
strings_test.go add splitn function Jul 11, 2018

README.md

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. This library provides a group of commonly used template functions.

It is inspired by the template functions found in Twig and also in various JavaScript libraries, such as underscore.js.

Usage

Template developers can read the Sprig function documentation to learn about the >100 template functions available.

For Go developers wishing to include Sprig as a library in their programs, API documentation is available at GoDoc.org, but read on for standard usage.

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

Call 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).

Example:

{{ "hello!" | upper | repeat 5 }}

Produces:

HELLO!HELLO!HELLO!HELLO!HELLO!

Principles:

The following principles were used in deciding on which functions to add, and determining how to implement them.

  • Template functions should be used to build layout. Therefore, 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 that can be displayed.
  • 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.