-
Notifications
You must be signed in to change notification settings - Fork 42
/
parser.go
53 lines (43 loc) 路 1.23 KB
/
parser.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
package view
import (
"bytes"
"html/template"
htmlTemplate "html/template"
"net/url"
"path/filepath"
textTemplate "text/template"
)
var htmlTemplateFuncs = template.FuncMap{
"unescaped": unescaped,
"urlencode": urlencode,
"urldecode": urldecode,
}
func TextRender(tplPath string, viewData interface{}, includeFiles ...string) (res string) {
tplName := filepath.Base(tplPath)
showBuffer := bytes.NewBuffer([]byte{})
tmpl := textTemplate.Must(textTemplate.New(tplName).ParseFiles(tplPath))
_, _ = tmpl.ParseFiles(includeFiles...)
_ = tmpl.Execute(showBuffer, viewData)
res = showBuffer.String()
return
}
func HtmlRender(tplPath string, viewData interface{}, includeFiles ...string) (res string) {
tplName := filepath.Base(tplPath)
showBuffer := bytes.NewBuffer([]byte{})
tmpl := htmlTemplate.Must(htmlTemplate.New(tplName).Funcs(htmlTemplateFuncs).ParseFiles(tplPath))
_, _ = tmpl.ParseFiles(includeFiles...)
_ = tmpl.Execute(showBuffer, viewData)
res = showBuffer.String()
return
}
// 妯$増鍑芥暟
func unescaped(x string) interface{} {
return template.HTML(x)
}
func urlencode(x string) interface{} {
return template.URLQueryEscaper(x)
}
func urldecode(x string) interface{} {
decode, _ := url.PathUnescape(x)
return decode
}