forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmpl.go
103 lines (91 loc) · 2.61 KB
/
tmpl.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Package tmpl contains functions for defining or executing in-line templates.
package tmpl
import (
"bytes"
"text/template"
"github.com/pkg/errors"
)
// Template -
type Template struct {
root *template.Template
defaultCtx interface{}
}
// New -
func New(root *template.Template, ctx interface{}) *Template {
return &Template{root, ctx}
}
// Inline - a template function to do inline template processing
//
// Can be called 4 ways:
// {{ tmpl.Inline "inline template" }} - unnamed (single-use) template with default context
// {{ tmpl.Inline "name" "inline template" }} - named template with default context
// {{ tmpl.Inline "inline template" $foo }} - unnamed (single-use) template with given context
// {{ tmpl.Inline "name" "inline template" $foo }} - named template with given context
func (t *Template) Inline(args ...interface{}) (string, error) {
name, in, ctx, err := t.parseArgs(args...)
if err != nil {
return "", err
}
return t.inline(name, in, ctx)
}
func (t *Template) inline(name, in string, ctx interface{}) (string, error) {
tmpl, err := t.root.New(name).Parse(in)
if err != nil {
return "", err
}
return render(tmpl, ctx)
}
// Exec - execute (render) a template - this is the built-in `template` action, except with output...
func (t *Template) Exec(name string, context ...interface{}) (string, error) {
ctx := t.defaultCtx
if len(context) == 1 {
ctx = context[0]
}
tmpl := t.root.Lookup(name)
if tmpl == nil {
return "", errors.Errorf(`template "%s" not defined`, name)
}
return render(tmpl, ctx)
}
func render(tmpl *template.Template, ctx interface{}) (string, error) {
out := &bytes.Buffer{}
err := tmpl.Execute(out, ctx)
if err != nil {
return "", err
}
return out.String(), nil
}
func (t *Template) parseArgs(args ...interface{}) (name, in string, ctx interface{}, err error) {
name = "<inline>"
ctx = t.defaultCtx
if len(args) == 0 || len(args) > 3 {
return "", "", nil, errors.Errorf("wrong number of args for tpl: want 1, 2, or 3 - got %d", len(args))
}
first, ok := args[0].(string)
if !ok {
return "", "", nil, errors.Errorf("wrong input: first arg must be string, got %T", args[0])
}
switch len(args) {
case 1:
in = first
case 2:
// this can either be (name string, in string) or (in string, ctx interface{})
switch second := args[1].(type) {
case string:
name = first
in = second
default:
in = first
ctx = second
}
case 3:
name = first
var ok bool
in, ok = args[1].(string)
if !ok {
return "", "", nil, errors.Errorf("wrong input: second arg (in) must be string, got %T", args[0])
}
ctx = args[2]
}
return name, in, ctx, nil
}