/
template.go
68 lines (59 loc) · 1.56 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
59
60
61
62
63
64
65
66
67
68
package beeplus
import (
"github.com/fplib-go/fplib"
"fmt"
"html/template"
"github.com/astaxie/beego"
)
func init() {
beego.AddFuncMap("LoadCSS", func(name string, c interface{}) template.HTML {
if name == "" {
name = "base"
}
var config RouterConfig
for key, value := range c.(map[interface{}]interface{}) {
strKey := fmt.Sprintf("%v", key)
if strKey == "RouterConfig" {
config = value.(RouterConfig)
break
}
}
html := config.LibLoader.LoadCSS(name, "/"+config.WebRoot)
return template.HTML(html)
})
beego.AddFuncMap("LoadJS", func(name string, c interface{}) template.HTML {
if name == "" {
name = "base"
}
var config RouterConfig
for key, value := range c.(map[interface{}]interface{}) {
strKey := fmt.Sprintf("%v", key)
if strKey == "RouterConfig" {
config = value.(RouterConfig)
break
}
}
html := config.LibLoader.LoadJS(name, "/"+config.WebRoot)
return template.HTML(html)
})
beego.AddFuncMap("json", func(o interface{}) template.JS {
if s, ok := o.(string); ok {
s = fplib.Trim(s)
if fplib.Str.StartWith(s, "{") || fplib.Str.StartWith(s, "[") {
if fplib.Str.EndWith(s, "}") || fplib.Str.EndWith(s, "]") || fplib.Str.EndWith(s, ";") || fplib.Str.EndWith(s, ",") {
fplib.Debug("isjsonstr:", template.JS(s))
return template.JS(s)
}
}
}
str := fplib.JSON.Stringify(o)
if str == "null" {
str = "{}"
}
return template.JS(str)
})
beego.AddFuncMap("site", func(key string) template.HTML {
v := beego.AppConfig.String("site::" + key)
return template.HTML(v)
})
}