-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrtpl.go
94 lines (73 loc) · 3.01 KB
/
strtpl.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
package strtpl
import (
"bytes"
htmlTemplate "html/template"
textTemplate "text/template"
)
// Eval applies Golang's text templating functions on a string with given data and returns the resulting string.
func Eval(templateString string, data interface{}) (output string, err error) {
return evalTextTemplate(templateString, textTemplate.FuncMap{}, data)
}
// MustEval applies Golang's text templating functions on a string with given data and returns the resulting string.
// In case of errors on the way, this function panics.
func MustEval(templateString string, data interface{}) (output string) {
return must(Eval(templateString, data))
}
// EvalWithFuncMap allows combining Eval with a custom FuncMap.
func EvalWithFuncMap(templateString string, funcs textTemplate.FuncMap, data interface{}) (output string, err error) {
return evalTextTemplate(templateString, funcs, data)
}
// MustEvalWithFuncMap allows combining MustEval with a custom FuncMap.
func MustEvalWithFuncMap(templateString string, funcs textTemplate.FuncMap, data interface{}) (output string) {
return must(EvalWithFuncMap(templateString, funcs, data))
}
// EvalHTML applies Golang's html templating functions on a string with given data and returns the resulting string.
func EvalHTML(templateString string, data interface{}) (output string, err error) {
return evalHTMLTemplate(templateString, htmlTemplate.FuncMap{}, data)
}
// MustEvalHTML applies Golang's html templating functions on a string with given data and returns the resulting string.
// In case of errors on the way, this function panics.
func MustEvalHTML(templateString string, data interface{}) (output string) {
return must(EvalHTML(templateString, data))
}
// EvalHTMLWithFuncMap allows combining EvalHTML with a custom FuncMap.
func EvalHTMLWithFuncMap(templateString string, funcs htmlTemplate.FuncMap, data interface{}) (output string, err error) {
return evalHTMLTemplate(templateString, funcs, data)
}
// MustEvalHTMLWithFuncMap allows combining MustEvalHTML with a custom FuncMap.
func MustEvalHTMLWithFuncMap(templateString string, funcs htmlTemplate.FuncMap, data interface{}) (output string) {
return must(EvalHTMLWithFuncMap(templateString, funcs, data))
}
// helper functions
func must(output string, err error) string {
if err != nil {
panic(err)
}
return output
}
func evalTextTemplate(templateString string, funcMap textTemplate.FuncMap, data interface{}) (output string, err error) {
var outputBuffer bytes.Buffer
t, err := textTemplate.New("tmpl").Funcs(funcMap).Parse(templateString)
if err != nil {
return
}
err = t.Execute(&outputBuffer, data)
if err != nil {
return
}
output = outputBuffer.String()
return
}
func evalHTMLTemplate(templateString string, funcMap htmlTemplate.FuncMap, data interface{}) (output string, err error) {
var outputBuffer bytes.Buffer
t, err := htmlTemplate.New("tmpl").Funcs(funcMap).Parse(templateString)
if err != nil {
return
}
err = t.Execute(&outputBuffer, data)
if err != nil {
return
}
output = outputBuffer.String()
return
}