forked from go-aah/aah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
232 lines (197 loc) · 7.34 KB
/
view.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
223
224
225
226
227
228
229
230
231
232
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// go-aah/aah source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package aah
import (
"fmt"
"html/template"
"path/filepath"
"strings"
"aahframework.org/essentials.v0"
"aahframework.org/view.v0"
)
const (
defaultViewEngineName = "go"
defaultViewFileExt = ".html"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// app methods
//______________________________________________________________________________
func (a *app) ViewEngine() view.Enginer {
if a.viewMgr == nil {
return nil
}
return a.viewMgr.engine
}
func (a *app) AddTemplateFunc(funcs template.FuncMap) {
view.AddTemplateFunc(funcs)
}
func (a *app) AddViewEngine(name string, engine view.Enginer) error {
return view.AddEngine(name, engine)
}
func (a *app) SetMinifier(fn MinifierFunc) {
if a.viewMgr == nil {
a.viewMgr = &viewManager{a: a, e: a.engine}
}
if a.viewMgr.minifier != nil {
a.Log().Warnf("Changing Minifier from: '%s' to '%s'", funcName(a.viewMgr.minifier), funcName(fn))
}
a.viewMgr.minifier = fn
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// app Unexported methods
//______________________________________________________________________________
func (a *app) initView() error {
viewsDir := filepath.Join(a.BaseDir(), "views")
if !ess.IsFileExists(viewsDir) {
// view directory not exists, scenario could be only API application
return nil
}
engineName := a.Config().StringDefault("view.engine", defaultViewEngineName)
viewEngine, found := view.GetEngine(engineName)
if !found {
return fmt.Errorf("view: named engine not found: %s", engineName)
}
viewMgr := &viewManager{
a: a,
e: a.engine,
engineName: engineName,
fileExt: a.Config().StringDefault("view.ext", defaultViewFileExt),
defaultTmplLayout: "master" + a.Config().StringDefault("view.ext", defaultViewFileExt),
filenameCaseSensitive: a.Config().BoolDefault("view.case_sensitive", false),
defaultLayoutEnabled: a.Config().BoolDefault("view.default_layout", true),
notFoundTmpl: template.Must(template.New("not_found").Parse(`
<strong>{{ .ViewNotFound }}</strong>
`)),
}
// Add Framework template methods
a.AddTemplateFunc(template.FuncMap{
"config": viewMgr.tmplConfig,
"i18n": viewMgr.tmplI18n,
"rurl": viewMgr.tmplURL,
"rurlm": viewMgr.tmplURLm,
"pparam": viewMgr.tmplPathParam,
"fparam": viewMgr.tmplFormParam,
"qparam": viewMgr.tmplQueryParam,
"session": viewMgr.tmplSessionValue,
"flash": viewMgr.tmplFlashValue,
"isauthenticated": viewMgr.tmplIsAuthenticated,
"hasrole": viewMgr.tmplHasRole,
"hasallroles": viewMgr.tmplHasAllRoles,
"hasanyrole": viewMgr.tmplHasAnyRole,
"ispermitted": viewMgr.tmplIsPermitted,
"ispermittedall": viewMgr.tmplIsPermittedAll,
"anitcsrftoken": viewMgr.tmplAntiCSRFToken,
})
if err := viewEngine.Init(a.Config(), viewsDir); err != nil {
return err
}
viewMgr.engine = viewEngine
if a.viewMgr != nil && a.viewMgr.minifier != nil {
viewMgr.minifier = a.viewMgr.minifier
}
a.viewMgr = viewMgr
return nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// View Manager
//______________________________________________________________________________
type viewManager struct {
a *app
e *engine
engineName string
engine view.Enginer
fileExt string
defaultTmplLayout string
filenameCaseSensitive bool
defaultLayoutEnabled bool
notFoundTmpl *template.Template
minifier MinifierFunc
}
// resolve method resolves the view template based available facts, such as
// controller name, action and user provided inputs.
func (vm *viewManager) resolve(ctx *Context) {
// Resolving view by convention and configuration
reply := ctx.Reply()
if reply.Rdr == nil {
reply.Rdr = &htmlRender{}
}
htmlRdr := reply.Rdr.(*htmlRender)
if htmlRdr.Template != nil {
// template already populated in it, no need to go forward
return
}
if ess.IsStrEmpty(htmlRdr.Layout) && vm.defaultLayoutEnabled {
htmlRdr.Layout = vm.defaultTmplLayout
}
if htmlRdr.ViewArgs == nil {
htmlRdr.ViewArgs = make(map[string]interface{})
}
for k, v := range ctx.ViewArgs() {
if _, found := htmlRdr.ViewArgs[k]; found {
continue
}
htmlRdr.ViewArgs[k] = v
}
// Add ViewArgs values from framework
vm.addFrameworkValuesIntoViewArgs(ctx)
var tmplPath, tmplName string
// If user not provided the template info, auto resolve by convention
if ess.IsStrEmpty(htmlRdr.Filename) {
tmplName = ctx.action.Name + vm.fileExt
tmplPath = filepath.Join(ctx.controller.Namespace, ctx.controller.NoSuffixName)
} else {
// User provided view info like layout, filename.
// Taking full-control of view rendering.
// Scenario's:
// 1. filename with relative path
// 2. filename with root page path
tmplName = filepath.Base(htmlRdr.Filename)
tmplPath = filepath.Dir(htmlRdr.Filename)
if strings.HasPrefix(htmlRdr.Filename, "/") {
tmplPath = strings.TrimLeft(tmplPath, "/")
} else {
tmplPath = filepath.Join(ctx.controller.Namespace, ctx.controller.NoSuffixName, tmplPath)
}
}
tmplPath = filepath.Join("pages", tmplPath)
ctx.Log().Tracef("Layout: %s, Template Path: %s, Template Name: %s", htmlRdr.Layout, tmplPath, tmplName)
var err error
if htmlRdr.Template, err = vm.engine.Get(htmlRdr.Layout, tmplPath, tmplName); err != nil {
if err == view.ErrTemplateNotFound {
tmplFile := filepath.Join("views", tmplPath, tmplName)
if !vm.filenameCaseSensitive {
tmplFile = strings.ToLower(tmplFile)
}
ctx.Log().Errorf("template not found: %s", tmplFile)
if vm.a.IsProfileProd() {
htmlRdr.ViewArgs["ViewNotFound"] = "View Not Found"
} else {
htmlRdr.ViewArgs["ViewNotFound"] = "View Not Found: " + tmplFile
}
htmlRdr.Layout = ""
htmlRdr.Template = vm.notFoundTmpl
} else {
ctx.Log().Error(err)
}
}
}
func (vm *viewManager) addFrameworkValuesIntoViewArgs(ctx *Context) {
html := ctx.Reply().Rdr.(*htmlRender)
html.ViewArgs["Scheme"] = ctx.Req.Scheme
html.ViewArgs["Host"] = ctx.Req.Host
html.ViewArgs["HTTPMethod"] = ctx.Req.Method
html.ViewArgs["RequestPath"] = ctx.Req.Path
html.ViewArgs["Locale"] = ctx.Req.Locale()
html.ViewArgs["ClientIP"] = ctx.Req.ClientIP()
html.ViewArgs["IsJSONP"] = ctx.Req.IsJSONP()
html.ViewArgs["IsAJAX"] = ctx.Req.IsAJAX()
html.ViewArgs["HTTPReferer"] = ctx.Req.Referer
html.ViewArgs["AahVersion"] = Version
html.ViewArgs[KeyViewArgRequestParams] = ctx.Req.Params
if ctx.subject != nil {
html.ViewArgs[KeyViewArgSubject] = ctx.Subject()
}
html.ViewArgs["EnvProfile"] = vm.a.Profile()
html.ViewArgs["AppBuildInfo"] = vm.a.BuildInfo()
}