forked from jiaoge/sjet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
164 lines (128 loc) · 3.63 KB
/
context.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
package context
import (
"reflect"
"strconv"
"strings"
"github.com/CloudyKit/jet/v6"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/shoppehub/sjet/engine"
)
// 模板引擎配置
type TemplateContext struct {
Vars *jet.VarMap
Context *map[string]interface{}
Template *jet.Template
TempatePath string
}
var TemplateRoot = "pages"
func (ctx *TemplateContext) FindTemplate(t *engine.TemplateEngine) error {
// templatePath := strings.Join([]string{TemplateRoot, ctx.Module, ctx.Page, ctx.TemplName}, "/")
var view *jet.Template
var err error
if view, err = t.Views.GetTemplate(TemplateRoot + "/" + ctx.TempatePath); err != nil {
ctx.TempatePath += "/index"
if view, err = t.Views.GetTemplate(TemplateRoot + "/" + ctx.TempatePath); err != nil {
return err
}
}
ctx.Template = view
ctx.Vars.Set("namespace", strings.ReplaceAll(ctx.TempatePath, "/", "_"))
return nil
}
// 初始化模板
func InitTemplateContext(t *engine.TemplateEngine, c *gin.Context) *TemplateContext {
vars := make(jet.VarMap)
handlerGetCtx(&vars, c)
context := make(map[string]interface{})
handlerContext(&vars, &context)
handlerRoute(&vars, c)
ctxData := TemplateContext{
Vars: &vars,
Context: &context,
}
ctxData.TempatePath = strings.TrimPrefix(c.Request.URL.Path, "/")
// handlerTemplateFile(c, &ctxData)
return &ctxData
}
// 解析模板路径 /:module/:page/:templ
func handlerTemplateFile(c *gin.Context, ctx *TemplateContext) {
ctx.TempatePath = strings.TrimPrefix(c.Request.URL.Path, "/")
}
func getParamInContext(key string, c *gin.Context, body *map[string]interface{}) interface{} {
if val, ok := c.GetQuery(key); ok {
return val
}
if val, ok := c.GetPostForm(key); ok {
return val
}
if val, ok := c.Params.Get(key); ok {
return val
}
bd := *body
if value, ok := bd[key]; ok {
return value
}
if value, ok := c.Get(key); ok {
return value
}
return ""
}
func handlerGetCtx(vars *jet.VarMap, c *gin.Context) {
body := make(map[string]interface{})
if c.Request.Body != nil {
c.ShouldBindBodyWith(&body, binding.JSON)
}
vars.SetFunc("getBody", func(a jet.Arguments) reflect.Value {
return reflect.ValueOf(&body)
})
vars.SetFunc("getCtx", func(a jet.Arguments) reflect.Value {
key := a.Get(0).String()
return reflect.ValueOf(getParamInContext(key, c, &body))
})
vars.SetFunc("getCtxForInt", func(a jet.Arguments) reflect.Value {
key := a.Get(0).String()
val := getParamInContext(key, c, &body)
if val == "" {
return reflect.ValueOf(0)
}
val, _ = strconv.ParseInt(val.(string), 10, 64)
return reflect.ValueOf(val)
})
vars.SetFunc("getCtxForFloat", func(a jet.Arguments) reflect.Value {
key := a.Get(0).String()
val := getParamInContext(key, c, &body)
if val == "" {
val = float64(0)
return reflect.ValueOf(val)
}
val, _ = strconv.ParseFloat(val.(string), 64)
return reflect.ValueOf(val)
})
vars.SetFunc("getCtxForBool", func(a jet.Arguments) reflect.Value {
key := a.Get(0).String()
val := getParamInContext(key, c, &body)
if val == "" {
return reflect.ValueOf(false)
}
val, _ = strconv.ParseBool(val.(string))
return reflect.ValueOf(val)
})
vars.SetFunc("getURL", func(a jet.Arguments) reflect.Value {
return reflect.ValueOf(c.Request.URL)
})
}
func handlerContext(vars *jet.VarMap, context *map[string]interface{}) {
vars.SetFunc("context", func(a jet.Arguments) reflect.Value {
ctx := *context
if a.NumOfArguments() == 1 {
if val, ok := ctx[a.Get(0).String()]; ok {
return reflect.ValueOf(val)
}
return reflect.Value{}
}
ctx[a.Get(0).String()] = a.Get(1).Interface()
*context = ctx
return reflect.Value{}
})
}