-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin.go
52 lines (43 loc) · 1.12 KB
/
gin.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
package simplate
import (
"bytes"
"html/template"
"github.com/gin-gonic/gin"
"github.com/chalvern/sugar"
"github.com/gin-gonic/gin/render"
)
var (
// GinRender the render of Gin
GinRender = &GinRendererS{}
)
// for gin https://github.com/gin-gonic/gin
// GinRendererS html render of gin
type GinRendererS struct {
}
// Instance method of HTMLRender
// https://github.com/gin-gonic/gin/blob/9a820cf0054bcd769f785457b7dbd149a7b29fdd/render/html.go#L21
func (hr *GinRendererS) Instance(name string, data interface{}) render.Render {
if simplateViewPathTemplates[name] == nil {
sugar.Warnf("no template of name: %s", name)
}
layoutFile := defaultLayoutFile
// body
var buf bytes.Buffer
ExecuteViewPathTemplate(&buf, name, data)
dataT := make(gin.H)
dataMap, ok := data.(gin.H)
if ok {
dataMap["LayoutContent"] = template.HTML(buf.String())
dataT = dataMap
// custom layout
if layout, ok := dataMap["layout"]; ok {
layoutFile = layout.(string)
}
} else {
dataT["LayoutContent"] = template.HTML(buf.String())
}
return render.HTML{
Template: simplateViewPathTemplates[layoutFile],
Data: dataT,
}
}