-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
58 lines (48 loc) · 1.48 KB
/
template.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
package helper
import (
"bytes"
"embed"
"github.com/pkg/errors"
"html/template"
)
func ParseTemplate(embedFS embed.FS, file string, args interface{}, additionalBodyContent ...string) (bytes.Buffer, error) {
var templateValue []byte
var err error
var tpl bytes.Buffer
if args != nil {
// read the block-kit definition as a go template
t, err := template.ParseFS(embedFS, file)
if err != nil {
return tpl, errors.Wrap(err, "phastos.go.helper.template.ParseTemplate.ParseFS")
}
for _, content := range additionalBodyContent {
tpl.Write([]byte(content))
}
// we render the view
err = t.Execute(&tpl, args)
if err != nil {
return tpl, errors.Wrap(err, "phastos.go.helper.template.ParseTemplate.ExecuteTemplate")
}
} else {
if templateValue, err = embedFS.ReadFile(file); err != nil {
return tpl, errors.Wrap(err, "phastos.go.helper.template.ParseTemplate.ReadFile")
}
tpl.Write(templateValue)
}
return tpl, nil
}
func ParseFileTemplate(filePath string, args interface{}, additionalBodyContent ...string) (bytes.Buffer, error) {
var err error
var tpl bytes.Buffer
t, err := template.ParseFiles(filePath)
if err != nil {
return tpl, errors.Wrap(err, "phastos.go.helper.template.ParseFileTemplate.ParseFiles")
}
for _, content := range additionalBodyContent {
tpl.Write([]byte(content))
}
if err = t.Execute(&tpl, args); err != nil {
return tpl, errors.Wrap(err, "phastos.go.helper.template.ParseFileTemplate.ExecuteTemplate")
}
return tpl, nil
}