-
Notifications
You must be signed in to change notification settings - Fork 15
/
render.go
147 lines (124 loc) · 4.45 KB
/
render.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
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
138
139
140
141
142
143
144
145
146
147
// Package render support to render templates by your control.
package render
import (
"html/template"
"net/http"
"path/filepath"
"github.com/qor/assetfs"
"github.com/qor/qor/utils"
)
// DefaultLayout default layout name
const DefaultLayout = "application"
// DefaultViewPath default view path
const DefaultViewPath = "app/views"
// Config render config
type Config struct {
ViewPaths []string
DefaultLayout string
FuncMapMaker func(render *Render, request *http.Request, writer http.ResponseWriter) template.FuncMap
AssetFileSystem assetfs.Interface
}
// Render the render struct.
type Render struct {
*Config
funcMaps template.FuncMap
}
// New initalize the render struct.
func New(config *Config, viewPaths ...string) *Render {
if config == nil {
config = &Config{}
}
if config.DefaultLayout == "" {
config.DefaultLayout = DefaultLayout
}
if config.AssetFileSystem == nil {
config.AssetFileSystem = assetfs.AssetFS().NameSpace("views")
}
config.ViewPaths = append(append(config.ViewPaths, viewPaths...), DefaultViewPath)
render := &Render{funcMaps: map[string]interface{}{}, Config: config}
for _, viewPath := range config.ViewPaths {
render.RegisterViewPath(viewPath)
}
return render
}
// RegisterViewPath register view path
func (render *Render) RegisterViewPath(paths ...string) {
for _, pth := range paths {
if filepath.IsAbs(pth) {
render.ViewPaths = append(render.ViewPaths, pth)
render.AssetFileSystem.RegisterPath(pth)
} else {
if absPath, err := filepath.Abs(pth); err == nil && isExistingDir(absPath) {
render.ViewPaths = append(render.ViewPaths, absPath)
render.AssetFileSystem.RegisterPath(absPath)
} else if isExistingDir(filepath.Join(utils.AppRoot, "vendor", pth)) {
render.AssetFileSystem.RegisterPath(filepath.Join(utils.AppRoot, "vendor", pth))
} else if p := filepath.Join(utils.AppRoot, pth); isExistingDir(p) { // This allows configuring correct viewpath in test. especially for test located in deep directory
render.ViewPaths = append([]string{p}, render.ViewPaths...)
render.AssetFileSystem.PrependPath(filepath.Join(utils.AppRoot, pth))
} else {
for _, gopath := range utils.GOPATH() {
if p := filepath.Join(gopath, "src", pth); isExistingDir(p) {
render.ViewPaths = append(render.ViewPaths, p)
render.AssetFileSystem.RegisterPath(p)
}
}
}
}
}
}
// PrependViewPath prepend view path
func (render *Render) PrependViewPath(paths ...string) {
for _, pth := range paths {
if filepath.IsAbs(pth) {
render.ViewPaths = append([]string{pth}, render.ViewPaths...)
render.AssetFileSystem.PrependPath(pth)
} else {
if absPath, err := filepath.Abs(pth); err == nil && isExistingDir(absPath) {
render.ViewPaths = append([]string{absPath}, render.ViewPaths...)
render.AssetFileSystem.PrependPath(absPath)
} else if isExistingDir(filepath.Join(utils.AppRoot, "vendor", pth)) {
render.AssetFileSystem.PrependPath(filepath.Join(utils.AppRoot, "vendor", pth))
} else {
for _, gopath := range utils.GOPATH() {
if p := filepath.Join(gopath, "src", pth); isExistingDir(p) {
render.ViewPaths = append([]string{p}, render.ViewPaths...)
render.AssetFileSystem.PrependPath(p)
}
}
}
}
}
}
// SetAssetFS set asset fs for render
func (render *Render) SetAssetFS(assetFS assetfs.Interface) {
for _, viewPath := range render.ViewPaths {
assetFS.RegisterPath(viewPath)
}
render.AssetFileSystem = assetFS
}
// Layout set layout for template.
func (render *Render) Layout(name string) *Template {
return &Template{render: render, layout: name}
}
// Funcs set helper functions for template with default "application" layout.
func (render *Render) Funcs(funcMap template.FuncMap) *Template {
tmpl := &Template{render: render, usingDefaultLayout: true}
return tmpl.Funcs(funcMap)
}
// Execute render template with default "application" layout.
func (render *Render) Execute(name string, context interface{}, request *http.Request, writer http.ResponseWriter) error {
tmpl := &Template{render: render, usingDefaultLayout: true}
return tmpl.Execute(name, context, request, writer)
}
// RegisterFuncMap register FuncMap for render.
func (render *Render) RegisterFuncMap(name string, fc interface{}) {
if render.funcMaps == nil {
render.funcMaps = template.FuncMap{}
}
render.funcMaps[name] = fc
}
// Asset get content from AssetFS by name
func (render *Render) Asset(name string) ([]byte, error) {
return render.AssetFileSystem.Asset(name)
}