diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2022-04-09 16:36:58 -0400 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2022-04-10 15:27:15 -0400 |
| commit | ae1620810cbd82eadd71abb51ba2fea97cf50a59 (patch) | |
| tree | a907ef15a3959cf1ac7101ea1bc5becc038b6605 /template.go | |
| parent | 6c66d16b7ccb32636688c644b670141b5e5341aa (diff) | |
New tmpl.Path/tmpl.PathDir functions
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'template.go')
| -rw-r--r-- | template.go | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/template.go b/template.go index 9d66dee0..4f4d6d36 100644 --- a/template.go +++ b/template.go @@ -32,13 +32,27 @@ type tplate struct { modeOverride bool } -func addTmplFuncs(f template.FuncMap, root *template.Template, ctx interface{}) { - t := tmpl.New(root, ctx) +func addTmplFuncs(f template.FuncMap, root *template.Template, tctx interface{}, path string) { + t := tmpl.New(root, tctx, path) tns := func() *tmpl.Template { return t } f["tmpl"] = tns f["tpl"] = t.Inline } +// copyFuncMap - copies the template.FuncMap into a new map so we can modify it +// without affecting the original +func copyFuncMap(funcMap template.FuncMap) template.FuncMap { + if funcMap == nil { + return nil + } + + newFuncMap := make(template.FuncMap, len(funcMap)) + for k, v := range funcMap { + newFuncMap[k] = v + } + return newFuncMap +} + func (t *tplate) toGoTemplate(g *gomplate) (tmpl *template.Template, err error) { if g.rootTemplate != nil { tmpl = g.rootTemplate.New(t.name) @@ -47,9 +61,12 @@ func (t *tplate) toGoTemplate(g *gomplate) (tmpl *template.Template, err error) g.rootTemplate = tmpl } tmpl.Option("missingkey=error") + + funcMap := copyFuncMap(g.funcMap) + // the "tmpl" funcs get added here because they need access to the root template and context - addTmplFuncs(g.funcMap, g.rootTemplate, g.tmplctx) - tmpl.Funcs(g.funcMap) + addTmplFuncs(funcMap, g.rootTemplate, g.tmplctx, t.name) + tmpl.Funcs(funcMap) tmpl.Delims(g.leftDelim, g.rightDelim) _, err = tmpl.Parse(t.contents) if err != nil { |
