Add support for FuncMap

This commit is contained in:
Ivan 2025-07-10 23:03:23 +05:00
parent 7e7f721002
commit 7c8ffa33c0
Signed by: landlord
GPG key ID: 416A03BC4ADA4CD7

View file

@ -23,6 +23,7 @@ type Collection struct {
type Options struct {
// Files from shared collections will be available in every main template.
SharedCollections []Collection
FuncMap template.FuncMap
}
// Load templates from specified directories with default options.
@ -38,6 +39,7 @@ func LoadWithOptions(
options Options,
) (map[string]*template.Template, error) {
sharedTemplate := template.New("")
for _, stp := range options.SharedCollections {
files, err := getFiles(stp.RootDir, stp.Prefix)
if err != nil {
@ -45,7 +47,12 @@ func LoadWithOptions(
}
for name, contents := range files {
sharedTemplate, err = sharedTemplate.New(name).Parse(contents)
sharedTemplate = sharedTemplate.New(name)
if options.FuncMap != nil {
sharedTemplate = sharedTemplate.Funcs(options.FuncMap)
}
sharedTemplate, err = sharedTemplate.Parse(contents)
if err != nil {
return nil, err
}