From e810247f13bed533f0adbd718dc31e027908fd86 Mon Sep 17 00:00:00 2001 From: Elara6331 Date: Tue, 20 Feb 2024 12:10:23 -0800 Subject: [PATCH] Add the ability to ignore missing macros/templates in macro and include tags --- include_tag.go | 9 +++++++++ macro_tag.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/include_tag.go b/include_tag.go index 5a99fde..d2ebb03 100644 --- a/include_tag.go +++ b/include_tag.go @@ -29,8 +29,17 @@ func (it includeTag) Run(tc *TagContext, block, args []ast.Node) error { return tc.PosError(args[0], "invalid first argument type: %T (expected string)", val) } + ignoreMissing := false + if name[0] == '?' { + name = name[1:] + ignoreMissing = true + } + tmpl, ok := tc.t.ns.GetTemplate(name) if !ok { + if ignoreMissing { + return nil + } return tc.PosError(args[0], "no such template: %q", name) } diff --git a/macro_tag.go b/macro_tag.go index edc6f99..80097eb 100644 --- a/macro_tag.go +++ b/macro_tag.go @@ -20,6 +20,12 @@ func (mt macroTag) Run(tc *TagContext, block, args []ast.Node) error { return tc.PosError(args[0], "invalid first argument type: %T (expected string)", nameVal) } + ignoreMissing := false + if name[0] == '?' { + name = name[1:] + ignoreMissing = true + } + if len(block) == 0 { local := map[string]any{} @@ -40,6 +46,9 @@ func (mt macroTag) Run(tc *TagContext, block, args []ast.Node) error { macro, ok := tc.t.macros[name] if !ok { + if ignoreMissing { + return nil + } return tc.PosError(tc.Tag, "no such macro: %q", name) } return tc.Execute(macro, local)