1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-11-08 15:30:05 +00:00
korvike/functions/collect.go

37 lines
861 B
Go
Raw Normal View History

// Package functions contains custom functions specific to korvike and
// returns the sprig functions with added korvike specific functions
2016-09-14 11:06:04 +00:00
package functions
import (
"log"
2016-09-14 11:06:04 +00:00
"sync"
"text/template"
"github.com/Masterminds/sprig/v3"
2016-09-14 11:06:04 +00:00
)
var (
templateFunctions = sprig.FuncMap()
2016-09-14 11:06:04 +00:00
templateFunctionsLock sync.Mutex
)
func registerFunction(name string, f interface{}) {
2016-09-14 11:06:04 +00:00
templateFunctionsLock.Lock()
defer templateFunctionsLock.Unlock()
2016-09-14 11:06:04 +00:00
if _, ok := templateFunctions[name]; ok {
log.Printf("overwriting existing function %q", name)
2016-09-14 11:06:04 +00:00
}
2016-09-14 11:06:04 +00:00
templateFunctions[name] = f
}
// GetFunctionMap exports all functions used in korvike to be used in own projects
// Example:
//
// import korvike "github.com/Luzifer/korvike"
// tpl := template.New("mytemplate").Funcs(korvike.GetFunctionMap())
2016-09-14 11:06:04 +00:00
func GetFunctionMap() template.FuncMap {
return templateFunctions
}