-
Notifications
You must be signed in to change notification settings - Fork 42
/
defaultengine.go
50 lines (42 loc) · 1.08 KB
/
defaultengine.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
package view
import (
"sync"
)
type DefaultEngine struct {
TemplatePath string
shareViewData map[string]interface{}
includeTmplName []string
mutx sync.Mutex
}
func NewDefaultViewEngine() *DefaultEngine {
return &DefaultEngine{}
}
func (c *DefaultEngine) SetTemplatePath(option *Option) {
c.TemplatePath = option.Path
c.includeTmplName = option.Includes
SetTemplatePath(option.Path)
}
// 全局通用模板变量
func (c *DefaultEngine) ShareViewData(key string, viewData interface{}) {
c.mutx.Lock()
defer c.mutx.Unlock()
c.shareViewData[key] = viewData
}
// 添加引入模版
func (c *DefaultEngine) AddIncludeTmpl(viewName string) {
c.mutx.Lock()
defer c.mutx.Unlock()
c.includeTmplName = append(c.includeTmplName, viewName)
}
// ViewHtml 快速渲染html视图
func (c *DefaultEngine) ViewHtml(viewName string, viewDataset ...interface{}) (string, error) {
tmpl := New(viewName, c.includeTmplName...)
for _, item := range viewDataset {
tmpl = tmpl.ViewData(item)
}
htmlRes, err := tmpl.Render()
if err != nil {
return htmlRes, err
}
return htmlRes, nil
}