Sprig

Go 模板的有用模板函数。「Useful template functions for Go templates.」

  • 所有者: Masterminds/sprig
  • 平台: Linux, Mac, Windows
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

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.8 of that package.
Using v0.3.9 will cause sprig tests to fail. The issue in mergo is tracked at
https://github.com/imdario/mergo/issues/139.

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

主要指标

概览
名称与所有者Masterminds/sprig
主编程语言Go
编程语言Go (语言数: 2)
平台Linux, Mac, Windows
许可证MIT License
所有者活动
创建于2013-11-22 01:20:40
推送于2024-10-28 11:50:27
最后一次提交2024-10-28 07:50:27
发布数38
最新版本名称v3.3.0 (发布于 2024-08-29 16:44:21)
第一版名称1.0.0 (发布于 )
用户参与
星数4.5k
关注者数36
派生数464
提交数487
已启用问题?
问题数188
打开的问题数107
拉请求数165
打开的拉请求数48
关闭的拉请求数39
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?