-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude_tag.go
64 lines (52 loc) · 1.41 KB
/
include_tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package salix
import (
"errors"
"go.elara.ws/salix/ast"
)
var (
ErrIncludeInvalidArgs = errors.New("include expects one string argument")
ErrNoSuchTemplate = errors.New("no such template")
)
// includeTag represents an #include tag within a Salix template
type includeTag struct{}
func (it includeTag) Run(tc *TagContext, block, args []ast.Node) error {
if len(args) < 1 {
return tc.PosError(tc.Tag, "expected at least one argument, got %d", len(args))
}
val, err := tc.GetValue(args[0], nil)
if err != nil {
return err
}
name, ok := val.(string)
if !ok {
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)
}
local := map[string]any{}
// Use the variable assignments after the first argument
// to set the local variables of the execution
for _, arg := range args[1:] {
if a, ok := arg.(ast.Assignment); ok {
val, err := tc.GetValue(a.Value, local)
if err != nil {
return err
}
local[a.Name.Value] = val
} else {
// If the argument isn't an assigment, return an error
return tc.PosError(tc.Tag, "invalid argument type: %T (expected ast.Assignment)", val)
}
}
return tc.Execute(tmpl.ast, local)
}