forked from jiaoge/sjet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
157 lines (128 loc) · 3.45 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
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
Module string
Page string
TemplName string
}
var TemplateRoot = "pages"
func (ctx *TemplateContext) Namespace() string {
return strings.Join([]string{ctx.Module, ctx.Page, ctx.TemplName}, "_")
}
func (ctx *TemplateContext) GetTemplateDir() string {
return strings.Join([]string{TemplateRoot, ctx.Module, ctx.Page}, "/")
}
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(templatePath); err != nil {
return err
}
ctx.Template = view
return nil
}
// 初始化模板
func InitTemplateContext(t *engine.TemplateEngine, c *gin.Context) *TemplateContext {
vars := make(jet.VarMap)
handlerGetCtx(&vars, c)
var context map[string]interface{}
handlerContext(&vars, &context)
ctxData := TemplateContext{
Vars: &vars,
Context: &context,
}
handlerTemplateFile(c, &ctxData)
vars.Set("namespace", ctxData.Namespace())
return &ctxData
}
// 解析模板路径 /:module/:page/:templ
func handlerTemplateFile(c *gin.Context, ctx *TemplateContext) {
module := c.Params.ByName("module")
if module == "" {
module = "index"
}
page := c.Params.ByName("page")
if page == "" {
page = "index"
}
templ := c.Params.ByName("templ")
if templ == "" {
templ = "index"
}
ctx.Module = module
ctx.Page = page
ctx.TemplName = templ
}
func getParamInContext(key string, c *gin.Context) 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
}
var body map[string]interface{}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err == nil {
return body[key]
}
return ""
}
func handlerGetCtx(vars *jet.VarMap, c *gin.Context) {
vars.SetFunc("getCtx", func(a jet.Arguments) reflect.Value {
key := a.Get(0).String()
return reflect.ValueOf(getParamInContext(key, c))
})
vars.SetFunc("getCtxForInt", func(a jet.Arguments) reflect.Value {
key := a.Get(0).String()
val := getParamInContext(key, c)
if val == "" {
return reflect.ValueOf(0)
}
val, _ = strconv.ParseInt(c.Query("curPage"), 10, 64)
return reflect.ValueOf(val)
})
vars.SetFunc("getCtxForFloat", func(a jet.Arguments) reflect.Value {
key := a.Get(0).String()
val := getParamInContext(key, c)
if val == "" {
val = float64(0)
return reflect.ValueOf(val)
}
val, _ = strconv.ParseFloat(c.Query("curPage"), 64)
return reflect.ValueOf(val)
})
vars.SetFunc("getCtxForBool", func(a jet.Arguments) reflect.Value {
key := a.Get(0).String()
val := getParamInContext(key, c)
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
ctx[a.Get(0).String()] = a.Get(1).Interface()
return reflect.ValueOf(&ctx)
})
}