• Stars
    star
    4,108
  • Rank 10,562 (Top 0.3 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 11 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Useful template functions for Go templates.

Sprig: Template functions for Go templates

GoDoc Go Report Card Stability: Sustained

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.

IMPORTANT NOTES

Sprig leverages 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 branch. Bug fixes to this major version will continue for some time.

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

More Repositories

1

glide

Package Management for Golang
Go
8,155
star
2

squirrel

Fluent SQL generation for golang
Go
6,755
star
3

html5-php

An HTML5 parser and serializer for PHP.
HTML
1,539
star
4

semver

Work with Semantic Versions in Go
Go
1,188
star
5

go-in-practice

Repository for Manning Publications Go in Practice
Go
353
star
6

structable

Golang struct-to-table database mapper
Go
293
star
7

vcs

VCS Repo management through a common interface in Go
Go
194
star
8

learning-helm

HTML
108
star
9

goutils

GoUtils is a Go implementation of some string manipulation libraries of Apache Commons. This is an open source project aimed at providing Go users with utility functions to manipulate strings in various ways.
Go
98
star
10

cookoo

A chain-of-command framework written in Go
Go
60
star
11

vert

Command line version testing: Compare versions at the CLI for use in shell scripts and make files.
Go
59
star
12

go-fileserver

A Go Fileserver where you can specify custom NotFound and Error response handlers.
Go
48
star
13

glide-report

Go
28
star
14

stability

Share the stability of a project
28
star
15

Fortissimo

A Chain Of Command (CoCo) framework for PHP.
PHP
25
star
16

log-go

A Golang logging interface with some reference implementations.
Go
21
star
17

kitt

Make your CLI apps pretty
Go
16
star
18

engine

An HTML theme engine for Go
Go
15
star
19

godir

Go Path Tool: A utility for working with Go and filesystem paths.
Go
12
star
20

codl

Codl: The Cookoo Domain Language
Go
11
star
21

Fortissimo-CLI-Base

A skeleton for creating Fortissimo CLI applications.
PHP
11
star
22

glide.sh

The website for Glide
HTML
10
star
23

convert

A mathematical conversion library.
Go
8
star
24

rmvcsdir

remove version control directories
Go
7
star
25

formenc

Decode form values using a Go unmarshal
Go
6
star
26

httputil

Framework-free HTTP utilities
Go
4
star
27

cookoo-cli-tutorial

A Git-based tutorial for using Cookoo to write command line apps.
Go
3
star
28

Villain

A CMS to be feared
PHP
3
star
29

cookoo-web-tutorial

A Git-based dive into building Cookoo web apps
Shell
3
star
30

Fortissimo-CLI

A CLI Setup for Fortissimo.
PHP
2
star
31

glide-ppa

Shell
2
star
32

Fortissimo-Base

A base setup for Fortissimo based site.
PHP
2
star
33

Fortissimo-Twig

Integration between Fortissimo and Twig
PHP
1
star
34

fortissimo-commons

Common commands for the Fortissimo framework.
PHP
1
star