Repository navigation
Expand file tree
/
Copy pathgview_config.go
More file actions
264 lines (245 loc) · 7.37 KB
/
Copy pathgview_config.go
File metadata and controls
264 lines (245 loc) · 7.37 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gview
import (
"errors"
"fmt"
"github.com/gogf/gf/i18n/gi18n"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/os/gres"
"github.com/gogf/gf/os/gspath"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
)
// Config is the configuration object for template engine.
type Config struct {
Paths []string // Searching array for path, NOT concurrent-safe for performance purpose.
Data map[string]interface{} // Global template variables including configuration.
DefaultFile string // Default template file for parsing.
Delimiters []string // Custom template delimiters.
AutoEncode bool // Automatically encodes and provides safe html output, which is good for avoiding XSS.
I18nManager *gi18n.Manager // I18n manager for the view.
}
const (
// Default template file for parsing.
defaultParsingFile = "index.html"
)
// DefaultConfig creates and returns a configuration object with default configurations.
func DefaultConfig() Config {
return Config{
DefaultFile: defaultParsingFile,
I18nManager: gi18n.Instance(),
Delimiters: make([]string, 2),
}
}
// SetConfig sets the configuration for view.
func (view *View) SetConfig(config Config) error {
var err error
if len(config.Paths) > 0 {
for _, v := range config.Paths {
if err = view.AddPath(v); err != nil {
return err
}
}
}
if len(config.Data) > 0 {
view.Assigns(config.Data)
}
if config.DefaultFile != "" {
view.SetDefaultFile(config.DefaultFile)
}
if len(config.Delimiters) > 1 {
view.SetDelimiters(config.Delimiters[0], config.Delimiters[1])
}
view.config = config
// Clear global template object cache.
// It's just cache, do not hesitate clearing it.
templates.Clear()
intlog.Printf("SetConfig: %+v", view.config)
return nil
}
// SetConfigWithMap set configurations with map for the view.
func (view *View) SetConfigWithMap(m map[string]interface{}) error {
if m == nil || len(m) == 0 {
return errors.New("configuration cannot be empty")
}
// The m now is a shallow copy of m.
// Any changes to m does not affect the original one.
// A little tricky, isn't it?
m = gutil.MapCopy(m)
// Most common used configuration support for single view path.
_, v1 := gutil.MapPossibleItemByKey(m, "paths")
_, v2 := gutil.MapPossibleItemByKey(m, "path")
if v1 == nil && v2 != nil {
m["paths"] = []interface{}{v2}
}
err := gconv.Struct(m, &view.config)
if err != nil {
return err
}
return view.SetConfig(view.config)
}
// SetPath sets the template directory path for template file search.
// The parameter <path> can be absolute or relative path, but absolute path is suggested.
func (view *View) SetPath(path string) error {
var (
isDir = false
realPath = ""
)
if file := gres.Get(path); file != nil {
realPath = path
isDir = file.FileInfo().IsDir()
} else {
// Absolute path.
realPath = gfile.RealPath(path)
if realPath == "" {
// Relative path.
view.paths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ := gspath.Search(v, path); path != "" {
realPath = path
break
}
}
})
}
if realPath != "" {
isDir = gfile.IsDir(realPath)
}
}
// Path not exist.
if realPath == "" {
err := errors.New(fmt.Sprintf(`[gview] SetPath failed: path "%s" does not exist`, path))
if errorPrint() {
glog.Error(err)
}
return err
}
// Should be a directory.
if !isDir {
err := errors.New(fmt.Sprintf(`[gview] SetPath failed: path "%s" should be directory type`, path))
if errorPrint() {
glog.Error(err)
}
return err
}
// Repeated path ad
A7BE
ding check.
if view.paths.Search(realPath) != -1 {
return nil
}
view.paths.Clear()
view.paths.Append(realPath)
view.fileCacheMap.Clear()
//glog.Debug("[gview] SetPath:", realPath)
return nil
}
// AddPath adds a absolute or relative path to the search paths.
func (view *View) AddPath(path string) error {
var (
isDir = false
realPath = ""
)
if file := gres.Get(path); file != nil {
realPath = path
isDir = file.FileInfo().IsDir()
} else {
// Absolute path.
realPath = gfile.RealPath(path)
if realPath == "" {
// Relative path.
view.paths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ := gspath.Search(v, path); path != "" {
realPath = path
break
}
}
})
}
if realPath != "" {
isDir = gfile.IsDir(realPath)
}
}
// Path not exist.
if realPath == "" {
err := errors.New(fmt.Sprintf(`[gview] AddPath failed: path "%s" does not exist`, path))
if errorPrint() {
glog.Error(err)
}
return err
}
// realPath should be type of folder.
if !isDir {
err := errors.New(fmt.Sprintf(`[gview] AddPath failed: path "%s" should be directory type`, path))
if errorPrint() {
glog.Error(err)
}
return err
}
// Repeated path adding check.
if view.paths.Search(realPath) != -1 {
return nil
}
view.paths.Append(realPath)
view.fileCacheMap.Clear()
return nil
}
// Assigns binds multiple global template variables to current view object.
// Note that it's not concurrent-safe, which means it would panic
// if it's called in multiple goroutines in runtime.
func (view *View) Assigns(data Params) {
for k, v := range data {
view.data[k] = v
}
}
// Assign binds a global template variable to current view object.
// Note that it's not concurrent-safe, which means it would panic
// if it's called in multiple goroutines in runtime.
func (view *View) Assign(key string, value interface{}) {
view.data[key] = value
}
// SetDefaultFile sets default template file for parsing.
func (view *View) SetDefaultFile(file string) {
view.config.DefaultFile = file
}
// GetDefaultFile returns default template file for parsing.
func (view *View) GetDefaultFile() string {
return view.config.DefaultFile
}
// SetDelimiters sets customized delimiters for template parsing.
func (view *View) SetDelimiters(left, right string) {
view.config.Delimiters = []string{left, right}
}
// SetAutoEncode enables/disables automatically html encoding feature.
// When AutoEncode feature is enables, view engine automatically encodes and provides safe html output,
// which is good for avoid XSS.
func (view *View) SetAutoEncode(enable bool) {
view.config.AutoEncode = enable
}
// BindFunc registers customized global template function named <name>
// with given function <function> to current view object.
// The <name> is the function name which can be called in template content.
func (view *View) BindFunc(name string, function interface{}) {
view.funcMap[name] = function
// Clear global template object cache.
templates.Clear()
}
// BindFuncMap registers customized global template functions by map to current view object.
// The key of map is the template function name
// and the value of map is the address of customized function.
func (view *View) BindFuncMap(funcMap FuncMap) {
for k, v := range funcMap {
view.funcMap[k] = v
}
// Clear global template object cache.
templates.Clear()
}
// SetI18n binds i18n manager to current view engine.
func (view *View) SetI18n(manager *gi18n.Manager) {
view.config.I18nManager = manager
}