diff --git a/namespace.go b/namespace.go index a15d3d3..58f4ce2 100644 --- a/namespace.go +++ b/namespace.go @@ -43,7 +43,13 @@ func New() *Namespace { func (n *Namespace) WithVarMap(m map[string]any) *Namespace { n.mu.Lock() defer n.mu.Unlock() - n.vars = m + + if m == nil { + n.vars = map[string]any{} + } else { + n.vars = m + } + return n } @@ -52,10 +58,10 @@ func (n *Namespace) WithTagMap(m map[string]Tag) *Namespace { n.mu.Lock() defer n.mu.Unlock() - if m != nil { - n.tags = m - } else { + if m == nil { n.tags = map[string]Tag{} + } else { + n.tags = m } return n diff --git a/salix.go b/salix.go index f943bda..1c3f6a0 100644 --- a/salix.go +++ b/salix.go @@ -67,17 +67,21 @@ type Template struct { // WithVarMap returns a copy of the template with its variable map set to m. func (t Template) WithVarMap(m map[string]any) Template { - t.vars = m + if m == nil { + t.vars = map[string]any{} + } else { + t.vars = m + } return t } // WithTagMap returns a copy of the template with its tag map set to m. func (t Template) WithTagMap(m map[string]Tag) Template { - // Make sure the tag map is never nil to avoid panics if m == nil { - m = map[string]Tag{} + t.tags = map[string]Tag{} + } else { + t.tags = m } - t.tags = m return t }