-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
128 lines (112 loc) · 3.53 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
// 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 gmvc
import (
"github.com/gogf/gf/frame/gins"
"sync"
"github.com/gogf/gf/util/gmode"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/os/gview"
)
// View is the view object for controller.
// It's initialized when controller request initializes and destroyed
// when the controller request closes.
type View struct {
mu sync.RWMutex
view *gview.View
data gview.Params
response *ghttp.Response
}
// NewView creates and returns a controller view object.
func NewView(w *ghttp.Response) *View {
return &View{
view: gins.View(),
data: make(gview.Params),
response: w,
}
}
// Assigns assigns template variables to this view object.
func (view *View) Assigns(data gview.Params) {
view.mu.Lock()
for k, v := range data {
view.data[k] = v
}
view.mu.Unlock()
}
// Assign assigns one template variable to this view object.
func (view *View) Assign(key string, value interface{}) {
view.mu.Lock()
view.data[key] = value
view.mu.Unlock()
}
// Parse parses given template file <tpl> with assigned template variables
// and returns the parsed template content.
func (view *View) Parse(file string) (string, error) {
view.mu.RLock()
defer view.mu.RUnlock()
buffer, err := view.response.ParseTpl(file, view.data)
return buffer, err
}
// ParseContent parses given template file <file> with assigned template variables
// and returns the parsed template content.
func (view *View) ParseContent(content string) (string, error) {
view.mu.RLock()
defer view.mu.RUnlock()
buffer, err := view.response.ParseTplContent(content, view.data)
return buffer, err
}
// LockFunc locks writing for template variables by callback function <f>.
func (view *View) LockFunc(f func(data gview.Params)) {
view.mu.Lock()
defer view.mu.Unlock()
f(view.data)
}
// LockFunc locks reading for template variables by callback function <f>.
func (view *View) RLockFunc(f func(data gview.Params)) {
view.mu.RLock()
defer view.mu.RUnlock()
f(view.data)
}
// BindFunc registers customized 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.view.BindFunc(name, function)
}
// BindFuncMap registers customized 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 gview.FuncMap) {
view.view.BindFuncMap(funcMap)
}
// Display parses and writes the parsed template file content to http response.
func (view *View) Display(file ...string) error {
name := view.view.GetDefaultFile()
if len(file) > 0 {
name = file[0]
}
if content, err := view.Parse(name); err != nil {
if !gmode.IsProduct() {
view.response.Write("Tpl Parsing Error: " + err.Error())
}
return err
} else {
view.response.Write(content)
}
return nil
}
// DisplayContent parses and writes the parsed content to http response.
func (view *View) DisplayContent(content string) error {
if content, err := view.ParseContent(content); err != nil {
if !gmode.IsProduct() {
view.response.Write("Tpl Parsing Error: " + err.Error())
}
return err
} else {
view.response.Write(content)
}
return nil
}