Masterminds/sprig

GitHub: Masterminds/sprig

Sprig 是一个为 Go 模板引擎提供 100 多个实用模板函数的扩展库,弥补标准库模板函数不足的问题。

Stars: 4731 | Forks: 478

# Sprig:Go 模板的模板函数 [![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://static.pigsec.cn/wp-content/uploads/repos/cas/09/097271ca091990be630ef6043309cc48240faa054413384202036fa2efedb2d2.svg)](https://github.com/Masterminds/sprig/actions) Go 语言自带了[内置的模板 语言](http://golang.org/pkg/text/template/),但模板 函数并不多。Sprig 是一个提供了 100 多个常用模板函数的库。 它的灵感来源于 [Twig](http://twig.sensiolabs.org/documentation) 中的模板函数以及各种 JavaScript 库,例如 [underscore.js](http://underscorejs.org/)。 ## 重要说明 Sprig 利用 [mergo](https://github.com/imdario/mergo) 来处理合并操作。在 其 v0.3.9 版本中,发生了一项影响 sprig 中合并模板 函数的行为变更。目前建议使用该包的 v0.3.10 或更高版本。 使用 v0.3.9 会导致 sprig 测试失败。 ## 包版本 目前有两个处于活跃维护状态的 `sprig` 包主版本。 * v3 是目前在 `master` 分支上的稳定发布系列。其 Go API 应该 与当前的稳定版本 v2 保持兼容。部分函数背后的 行为变更是发布此新主版本的原因。 * v2 是先前的稳定发布系列。自 v2 初次发布以来,已经过去了三年多的时间。 您可以阅读文档并查看 [release-2](https://github.com/Masterminds/sprig/tree/release-2) 分支上的代码。 对该主版本的 Bug 修复将持续一段时间。 ## 用法 **模板开发者**:请查阅 Sprig 的[函数文档](http://masterminds.github.io/sprig/), 以获取 >100 个可用模板函数的详细说明和代码片段。 **Go 开发者**:如果您想在您的程序中将 Sprig 作为库使用, 我们的 API 文档可在 [GoDoc.org](http://godoc.org/github.com/Masterminds/sprig) 获取。 有关标准用法,请继续阅读。 ### 加载 Sprig 库 要加载 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") ) ``` ### 在模板内部调用函数 按照惯例,所有函数均为小写。这似乎遵循了 Go 中关于 模板函数的惯用法(与使用 TitleCase 的模板方法相反)。例如,这样: ``` {{ "hello!" | upper | repeat 5 }} ``` 会生成: ``` HELLO!HELLO!HELLO!HELLO!HELLO! ``` ## 驱动我们函数选择的原则 我们遵循以下原则来决定添加哪些函数以及如何实现它们: - 使用模板函数来构建布局。以下 类型的操作属于模板函数的范畴: - 格式化 - 布局 - 简单的类型转换 - 协助处理常见格式化和布局需求(例如算术运算)的实用工具 - 除非无法输出合理的值,否则模板函数不应返回错误。例如, 将字符串转换为整数时,如果转换失败,不应 报错。相反,它应该显示一个默认 值。 - 简单的数学运算对于网格布局、分页器等是必要的。复杂的数学运算 (算术运算以外的任何内容)应该在模板之外完成。 - 模板函数仅处理传递给它们的数据。它们从不从 数据源检索数据。 - 最后,不要覆盖 Go 的核心模板函数。
标签:EVTX分析, Go语言, SOC Prime, 代码生成, 函数库, 开发工具, 日志审计, 模板引擎, 渗透测试工具, 程序破解