Add the ability to ignore missing macros/templates in macro and include tags

This commit is contained in:
Elara 2024-02-20 12:10:23 -08:00
parent d91eef09bf
commit e810247f13
2 changed files with 18 additions and 0 deletions

View File

@ -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) 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) tmpl, ok := tc.t.ns.GetTemplate(name)
if !ok { if !ok {
if ignoreMissing {
return nil
}
return tc.PosError(args[0], "no such template: %q", name) return tc.PosError(args[0], "no such template: %q", name)
} }

View File

@ -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) 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 { if len(block) == 0 {
local := map[string]any{} 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] macro, ok := tc.t.macros[name]
if !ok { if !ok {
if ignoreMissing {
return nil
}
return tc.PosError(tc.Tag, "no such macro: %q", name) return tc.PosError(tc.Tag, "no such macro: %q", name)
} }
return tc.Execute(macro, local) return tc.Execute(macro, local)