-
Notifications
You must be signed in to change notification settings - Fork 50
/
web_util.go
77 lines (71 loc) · 1.68 KB
/
web_util.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
package proxy
import (
"bytes"
"fmt"
"github.com/hidu/goutils"
"html"
"strings"
"text/template"
"time"
)
func readerHTMLInclude(fileName string) string {
html := Assest.GetContent("/res/tpl/" + fileName)
myfn := template.FuncMap{
"my_include": func(name string) string {
return readerHTMLInclude(name)
},
}
tpl, _ := template.New("page_include").Delims("{%", "%}").Funcs(myfn).Parse(html)
var bf []byte
w := bytes.NewBuffer(bf)
tpl.Execute(w, make(map[string]string))
body := w.String()
return body
}
func renderHTML(fileName string, values map[string]interface{}, layout bool) string {
htmlStr := readerHTMLInclude(fileName)
myfn := template.FuncMap{
"shortTime": func(tu int64) string {
t := time.Unix(tu, 0)
return t.Format(timeFormatStd)
},
"myNum": func(n int64) string {
if n == 0 {
return ""
}
return fmt.Sprintf("%d", n)
},
"in_array": func(name string, names []string) bool {
for _, v := range names {
if v == name {
return true
}
}
return false
},
"str_eq": func(x, y interface{}) bool {
ret := fmt.Sprintf("%x", x) == fmt.Sprintf("%x", y)
return ret
},
"my_include": func(fileName string) string {
return "include (" + fileName + ") with Delims {%my_include %}"
},
"h": func(str string) string {
return html.EscapeString(str)
},
"join": func(arr []string) string {
return strings.Join(arr, "\n")
},
}
tpl, _ := template.New("page").Funcs(myfn).Parse(htmlStr)
var bf []byte
w := bytes.NewBuffer(bf)
tpl.Execute(w, values)
body := w.String()
if layout {
values["body"] = body
return renderHTML("layout.html", values, false)
}
// return body
return utils.Html_reduceSpace(body)
}