Skip to content

Commit 6162c77

Browse files
authored
fix: bug: funcMap not unique per engine instance. (#2)
Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
1 parent c71bf6e commit 6162c77

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

engine.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func newEngine(fsys fs.FS, options ...Option) (Engine, error) {
4141
m := moldEngine{}
4242

4343
// traverse to fetch all templates and populate the root template.
44-
root, ts, err := walk(c.fs, c.exts.val)
44+
root, ts, err := walk(c.fs, c.exts.val, c.funcMap.val)
4545
if err != nil {
4646
return nil, fmt.Errorf("error creating new engine: %w", err)
4747
}
@@ -82,7 +82,7 @@ func (m moldEngine) Render(w io.Writer, view string, data any) error {
8282
return nil
8383
}
8484

85-
func walk(fsys fs.FS, exts []string) (root templateSet, ts []templateFile, err error) {
85+
func walk(fsys fs.FS, exts []string, funcMap template.FuncMap) (root templateSet, ts []templateFile, err error) {
8686
root = templateSet{}
8787
err = fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
8888
if err != nil {
@@ -111,7 +111,7 @@ func walk(fsys fs.FS, exts []string) (root templateSet, ts []templateFile, err e
111111
return err
112112
}
113113

114-
if t, err := template.New(path).Funcs(placeholderFuncs).Parse(f); err != nil {
114+
if t, err := template.New(path).Funcs(funcMap).Parse(f); err != nil {
115115
return fmt.Errorf("error parsing template '%s': %w", path, err)
116116
} else {
117117
root[path] = t
@@ -158,7 +158,7 @@ func setup(c *Config, options ...Option) error {
158158
}
159159

160160
// funcMap
161-
funcMap := placeholderFuncs
161+
funcMap := placeholderFuncs()
162162
if c.funcMap.set {
163163
for k, f := range c.funcMap.val {
164164
funcMap[k] = f
@@ -257,9 +257,11 @@ func validExt(exts []string, ext string) bool {
257257
return false
258258
}
259259

260-
var placeholderFuncs = map[string]any{
261-
renderFunc.String(): func(...string) string { return "" },
262-
partialFunc.String(): func(string, ...any) string { return "" },
260+
func placeholderFuncs() template.FuncMap {
261+
return map[string]any{
262+
renderFunc.String(): func(...string) string { return "" },
263+
partialFunc.String(): func(string, ...any) string { return "" },
264+
}
263265
}
264266

265267
type templateFile struct {

0 commit comments

Comments
 (0)