-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrender.go
More file actions
137 lines (114 loc) · 3.23 KB
/
render.go
File metadata and controls
137 lines (114 loc) · 3.23 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package widget
import (
"bytes"
"fmt"
"html/template"
"path/filepath"
"github.com/qor/qor/utils"
)
// Render find widget by name, render it based on current context
func (widgets *Widgets) Render(widgetName string, widgetGroupName string) template.HTML {
return widgets.NewContext(nil).Render(widgetName, widgetGroupName)
}
// NewContext create new context for widgets
func (widgets *Widgets) NewContext(context *Context) *Context {
if context == nil {
context = &Context{}
}
if context.DB == nil {
context.DB = widgets.Config.DB
}
if context.Options == nil {
context.Options = map[string]interface{}{}
}
if context.FuncMaps == nil {
context.FuncMaps = template.FuncMap{}
}
for key, fc := range widgets.funcMaps {
if _, ok := context.FuncMaps[key]; !ok {
context.FuncMaps[key] = fc
}
}
context.Widgets = widgets
return context
}
// Funcs return view functions map
func (context *Context) Funcs(funcMaps template.FuncMap) *Context {
if context.FuncMaps == nil {
context.FuncMaps = template.FuncMap{}
}
for key, fc := range funcMaps {
context.FuncMaps[key] = fc
}
return context
}
// FuncMap return funcmap
func (context *Context) FuncMap() template.FuncMap {
funcMap := template.FuncMap{}
funcMap["render_widget"] = func(widgetName string, widgetGroupName ...string) template.HTML {
var groupName string
if len(widgetGroupName) == 0 {
groupName = ""
} else {
groupName = widgetGroupName[0]
}
return context.Render(widgetName, groupName)
}
return funcMap
}
// Render register widget itself content
func (w *Widget) Render(context *Context, file string) template.HTML {
if len(context.Body) > 0 {
return template.HTML(context.Body)
}
var (
err error
content []byte
tmpl *template.Template
)
if file == "" && len(w.Templates) > 0 {
file = w.Templates[0]
}
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Get error when render file %v: %v", file, r)
utils.ExitWithMsg(err)
}
}()
if content, err = context.Widgets.AssetFS.Asset(file + ".tmpl"); err == nil {
if tmpl, err = template.New(filepath.Base(file)).Funcs(context.FuncMaps).Parse(string(content)); err == nil {
var result = bytes.NewBufferString("")
if err = tmpl.Execute(result, context.Options); err == nil {
return template.HTML(result.String())
}
}
}
return template.HTML(err.Error())
}
// RegisterViewPath register views directory
func (widgets *Widgets) RegisterViewPath(p string) {
if filepath.IsAbs(p) {
viewPaths = append(viewPaths, p)
widgets.AssetFS.RegisterPath(p)
} else {
for _, gopath := range utils.GOPATH() {
viewPaths = append(viewPaths, filepath.Join(gopath, "src", p))
widgets.AssetFS.RegisterPath(filepath.Join(gopath, "src", p))
}
}
}
// LoadPreviewAssets will return assets tag used for Preview
func (widgets *Widgets) LoadPreviewAssets() template.HTML {
tags := ""
for _, asset := range widgets.Config.PreviewAssets {
extension := filepath.Ext(asset)
if extension == ".css" {
tags += fmt.Sprintf("<link rel=\"stylesheet\" type=\"text/css\" href=\"%v\">\n", asset)
} else if extension == ".js" {
tags += fmt.Sprintf("<script src=\"%v\"></script>\n", asset)
} else {
tags += fmt.Sprintf("%v\n", asset)
}
}
return template.HTML(tags)
}