Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Masterminds/sprig
Useful template functions for Go templates.
https://github.com/Masterminds/sprig
go template templates
Last synced: about 2 months ago
JSON representation
Useful template functions for Go templates.
- Host: GitHub
- URL: https://github.com/Masterminds/sprig
- Owner: Masterminds
- License: mit
- Created: 2013-11-22T01:20:40.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2024-09-24T19:44:35.000Z (3 months ago)
- Last Synced: 2024-10-20T10:20:07.032Z (about 2 months ago)
- Topics: go, template, templates
- Language: Go
- Homepage: http://masterminds.github.io/sprig/
- Size: 572 KB
- Stars: 4,271
- Watchers: 38
- Forks: 441
- Open Issues: 134
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-go - sprig - Useful template functions for Go templates. (Template Engines / HTTP Clients)
- stars - Masterminds/sprig
- zero-alloc-awesome-go - sprig - Useful template functions for Go templates. (Template Engines / HTTP Clients)
- favorite-link - Go 模板的有用模板函数。
- my-awesome - Masterminds/sprig - 10 star:4.3k fork:0.4k Useful template functions for Go templates. (Go)
- awesome-ccamel - Masterminds/sprig - Useful template functions for Go templates. (Go)
- awesome-starts - Masterminds/sprig - Useful template functions for Go templates. (Go)
- go-awesome - Sprig - general template method (Open source library / Template Engine)
- awesome-go - sprig - Useful template functions for Go templates. Stars:`4.1K`. (Template Engines / HTTP Clients)
- awesome-golang-repositories - sprig
- awesome-hacking-lists - Masterminds/sprig - Useful template functions for Go templates. (Go)
- awesome-go-extra - sprig - 11-22T01:20:40Z|2022-08-17T08:46:20Z| (Template Engines / HTTP Clients)
- awesome - sprig - Useful template functions for Go templates. (Go)
README
# Sprig: Template functions for Go templates
[![GoDoc](https://img.shields.io/static/v1?label=godoc&message=reference&color=blue)](https://pkg.go.dev/github.com/Masterminds/sprig/v3)
[![Go Report Card](https://goreportcard.com/badge/github.com/Masterminds/sprig)](https://goreportcard.com/report/github.com/Masterminds/sprig)
[![Stability: Sustained](https://masterminds.github.io/stability/sustained.svg)](https://masterminds.github.io/stability/sustained.html)
[![](https://github.com/Masterminds/sprig/workflows/Tests/badge.svg)](https://github.com/Masterminds/sprig/actions)The Go language comes with a [built-in template
language](http://golang.org/pkg/text/template/), 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](http://twig.sensiolabs.org/documentation) and in various
JavaScript libraries, such as [underscore.js](http://underscorejs.org/).## IMPORTANT NOTES
Sprig leverages [mergo](https://github.com/imdario/mergo) to handle merges. In
its v0.3.9 release, there was a behavior change that impacts merging template
functions in sprig. It is currently recommended to use v0.3.10 or later of that package.
Using v0.3.9 will cause sprig tests to fail.## Package Versions
There are two active major versions of the `sprig` package.
* v3 is currently stable release series on the `master` branch. The Go API should
remain compatible with v2, the current stable version. Behavior change behind
some functions is the reason for the new major version.
* v2 is the previous stable release series. It has been more than three years since
the initial release of v2. You can read the documentation and see the code
on the [release-2](https://github.com/Masterminds/sprig/tree/release-2) branch.
Bug fixes to this major version will continue for some time.## Usage
**Template developers**: Please use Sprig's [function documentation](http://masterminds.github.io/sprig/) 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](http://godoc.org/github.com/Masterminds/sprig).For standard usage, read on.
### Load the Sprig library
To load the Sprig `FuncMap`:
```go
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")
)```
### 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.