-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathviewengine.go
222 lines (203 loc) · 6.47 KB
/
viewengine.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package goku
import (
"fmt"
"github.com/QLeelulu/goku/utils"
"html/template"
"io"
"path"
"strings"
)
type ViewData struct {
Data map[string]interface{}
Model interface{}
Globals map[string]interface{}
Body interface{} // if in layout template, this will set
}
// TemplateEnginer interface
type TemplateEnginer interface {
// render the view with viewData and write to w
Render(viewpath string, layoutPath string, viewData *ViewData, w io.Writer)
// return whether the tempalte support layout
SupportLayout() bool
// template file ext name, default is ".html"
Ext() string
}
// DefaultTemplateEngine
type DefaultTemplateEngine struct {
ExtName string
UseCache bool
TemplateCache map[string]*template.Template
}
// template file ext name, default is ".html"
func (te *DefaultTemplateEngine) Ext() string {
if te.ExtName == "" {
return ".html"
}
return te.ExtName
}
// SupportLayout returns whether the tempalte support layout
func (te *DefaultTemplateEngine) SupportLayout() bool {
return true
}
func (te *DefaultTemplateEngine) Render(filepath string, layoutPath string, viewData *ViewData, wr io.Writer) {
if te.SupportLayout() && layoutPath != "" {
te.render([]string{layoutPath, filepath}, viewData, wr)
} else {
te.render([]string{filepath}, viewData, wr)
}
}
func (te *DefaultTemplateEngine) render(filepaths []string, viewData interface{}, wr io.Writer) {
var tmpl *template.Template
var err error
cacheKey := strings.Join(filepaths, "_")
if te.UseCache {
tmpl = te.TemplateCache[cacheKey]
}
if tmpl == nil {
tmpl, err = template.ParseFiles(filepaths...)
if err != nil {
panic("DefaultTemplateEngine.Render: parse template \"" + strings.Join(filepaths, ", ") + "\" error, " + err.Error())
}
if te.UseCache {
te.TemplateCache[cacheKey] = tmpl
}
}
err = tmpl.Execute(wr, viewData)
if err != nil {
panic(err)
}
}
type ViewInfo struct {
Controller, Action, View, Layout string
IsPartial bool
}
// ViewEnginer interface.
// For how to find the view file.
type ViewEnginer interface {
// find the view and layout
// if template engine not suppot layout, just return empty string
FindView(vi *ViewInfo) (viewPath string, layoutPath string)
}
// DefaultViewEngine
type DefaultViewEngine struct {
ExtName string // template file ext name, default is ".html"
RootDir string // view's root dir, must set
Layout string // template layout name, default is "layout"
ViewLocationFormats []string
LayoutLocationFormats []string
UseCache bool // whether cache the viewfile
Caches map[string]string // controller & action & view to the real-file-path cache
}
func (ve *DefaultViewEngine) FindView(vi *ViewInfo) (viewPath string, layoutPath string) {
viewPath = ve.lookup(vi, false)
if !vi.IsPartial {
layoutPath = ve.lookup(vi, true)
}
return
}
func (ve *DefaultViewEngine) lookup(vi *ViewInfo, isLayout bool) string {
var viewName, cacheKey string
var locas []string
if !vi.IsPartial && isLayout {
viewName = vi.Layout
if vi.Layout == "" {
viewName = ve.Layout // default layout
} else {
viewName = vi.Layout
}
if viewName == "" {
return ""
}
cacheKey = vi.Controller + "_layout_" + viewName
locas = ve.LayoutLocationFormats
} else {
viewName = vi.View
if viewName == "" {
viewName = vi.Action
}
cacheKey = vi.Controller + "_" + viewName
locas = ve.ViewLocationFormats
}
viewName = viewName + ve.ExtName
if ve.UseCache {
if v, ok := ve.Caches[cacheKey]; ok {
return v
}
}
lookPaths := make([]string, 0, 3)
// Absolute path,
// direct use viewpath
if viewName[0] == '/' {
viewPath := path.Join(ve.RootDir, viewName)
if ok, _ := utils.FileExists(viewPath); ok {
ve.Caches[cacheKey] = viewPath
return viewPath
}
lookPaths = append(lookPaths, viewPath)
} else {
for _, format := range locas {
viewPath := strings.Replace(format, "{1}", vi.Controller, 1)
viewPath = strings.Replace(viewPath, "{0}", viewName, 1)
viewPath = path.Join(ve.RootDir, viewPath)
if ok, _ := utils.FileExists(viewPath); ok {
ve.Caches[cacheKey] = viewPath
return viewPath
}
lookPaths = append(lookPaths, viewPath)
}
}
if !isLayout {
panic(fmt.Sprintf("DefaultViewEngine: can't find the view for {controller: %s, action: %s, view: %s}, look up paths: %s",
vi.Controller, vi.Action, vi.View, lookPaths))
}
return ""
}
// create a default ViewEnginer.
// some default value:
// + Layout: "layout"
// + ExtName: ".html"
// + ViewLocationFormats: []string{"{1}/{0}", "shared/{0}"} , {1} is controller, {0} is action or a viewName
// + LayoutLocationFormats: []string{"{1}/{0}", "shared/{0}"}
func CreateDefaultViewEngine(viewDir, layout, extName string, useCache bool) *DefaultViewEngine {
if viewDir == "" {
panic("CreateDefaultViewEngine: viewDir can not be empty.")
}
dve := &DefaultViewEngine{
ExtName: extName,
RootDir: viewDir,
Layout: layout,
UseCache: useCache,
Caches: make(map[string]string),
}
// default location paths
dve.ViewLocationFormats = []string{
"{1}/{0}",
"shared/{0}",
}
dve.LayoutLocationFormats = []string{
"{1}/{0}",
"shared/{0}",
}
if dve.Layout == "" {
dve.Layout = "layout"
}
if dve.ExtName == "" {
dve.ExtName = ".html"
}
return dve
}
// create a default TemplateEnginer.
func CreateDefaultTemplateEngine(useCache bool) *DefaultTemplateEngine {
te := &DefaultTemplateEngine{
UseCache: useCache,
TemplateCache: make(map[string]*template.Template),
}
return te
}
var globalViewData map[string]interface{} = make(map[string]interface{})
// SetGlobalViewData adds a view data to the global,
// that all the view can use it
// by {{.Global.key}}
func SetGlobalViewData(key string, val interface{}) {
globalViewData[key] = val
}