This repository was archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtemplate.go
236 lines (205 loc) · 5.82 KB
/
template.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
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
/*
* Copyright 2018 Foolin. All rights reserved.
*
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*
*/
/*
Golang template for gin framework, Use golang html/template syntax,
Easy and simple to use for gin framework, See https://github.com/foolin/gin-template
for more information.
*/
package gintemplate
import (
"bytes"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
)
var (
htmlContentType = []string{"text/html; charset=utf-8"}
templateEngineKey = "github.com/foolin/gin-template/templateEngine"
DefaultConfig = TemplateConfig{
Root: "views",
Extension: ".html",
Master: "layouts/master",
Partials: []string{},
Funcs: make(template.FuncMap),
DisableCache: false,
Delims: Delims{Left: "{{", Right: "}}"},
}
)
type TemplateEngine struct {
config TemplateConfig
tplMap map[string]*template.Template
tplMutex sync.RWMutex
fileHandler FileHandler
}
type TemplateRender struct {
Engine *TemplateEngine
Name string
Data interface{}
}
type TemplateConfig struct {
Root string //view root
Extension string //template extension
Master string //template master
Partials []string //template partial, such as head, foot
Funcs template.FuncMap //template functions
DisableCache bool //disable cache, debug mode
Delims Delims //delimeters
}
type Delims struct {
Left string
Right string
}
type FileHandler func(config TemplateConfig, tplFile string) (content string, err error)
func New(config TemplateConfig) *TemplateEngine {
return &TemplateEngine{
config: config,
tplMap: make(map[string]*template.Template),
tplMutex: sync.RWMutex{},
fileHandler: DefaultFileHandler(),
}
}
func Default() *TemplateEngine {
return New(DefaultConfig)
}
// You should use helper func `Middleware()` to set the supplied
// TemplateEngine and make `HTML()` work validly.
func HTML(ctx *gin.Context, code int, name string, data interface{}) {
if val, ok := ctx.Get(templateEngineKey); ok {
if e, ok := val.(*TemplateEngine); ok {
e.HTML(ctx, code, name, data)
return
}
}
ctx.HTML(code, name, data)
}
//New gin middleware for func `gintemplate.HTML()`
func NewMiddleware(config TemplateConfig) gin.HandlerFunc {
return Middleware(New(config))
}
func Middleware(e *TemplateEngine) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set(templateEngineKey, e)
}
}
func (e *TemplateEngine) Instance(name string, data interface{}) render.Render {
return TemplateRender{
Engine: e,
Name: name,
Data: data,
}
}
func (e *TemplateEngine) HTML(ctx *gin.Context, code int, name string, data interface{}) {
instance := e.Instance(name, data)
ctx.Render(code, instance)
}
func (e *TemplateEngine) executeRender(out io.Writer, name string, data interface{}) error {
useMaster := true
if filepath.Ext(name) == e.config.Extension {
useMaster = false
name = strings.TrimSuffix(name, e.config.Extension)
}
return e.executeTemplate(out, name, data, useMaster)
}
func (e *TemplateEngine) executeTemplate(out io.Writer, name string, data interface{}, useMaster bool) error {
var tpl *template.Template
var err error
var ok bool
allFuncs := make(template.FuncMap, 0)
allFuncs["include"] = func(layout string) (template.HTML, error) {
buf := new(bytes.Buffer)
err := e.executeTemplate(buf, layout, data, false)
return template.HTML(buf.String()), err
}
// Get the plugin collection
for k, v := range e.config.Funcs {
allFuncs[k] = v
}
e.tplMutex.RLock()
tpl, ok = e.tplMap[name]
e.tplMutex.RUnlock()
exeName := name
if useMaster && e.config.Master != "" {
exeName = e.config.Master
}
if !ok || e.config.DisableCache {
tplList := make([]string, 0)
if useMaster {
//render()
if e.config.Master != "" {
tplList = append(tplList, e.config.Master)
}
}
tplList = append(tplList, name)
tplList = append(tplList, e.config.Partials...)
// Loop through each template and test the full path
tpl = template.New(name).Funcs(allFuncs).Delims(e.config.Delims.Left, e.config.Delims.Right)
for _, v := range tplList {
var data string
data, err = e.fileHandler(e.config, v)
if err != nil {
return err
}
var tmpl *template.Template
if v == name {
tmpl = tpl
} else {
tmpl = tpl.New(v)
}
_, err = tmpl.Parse(data)
if err != nil {
return fmt.Errorf("TemplateEngine render parser name:%v, error: %v", v, err)
}
}
e.tplMutex.Lock()
e.tplMap[name] = tpl
e.tplMutex.Unlock()
}
// Display the content to the screen
err = tpl.Funcs(allFuncs).ExecuteTemplate(out, exeName, data)
if err != nil {
return fmt.Errorf("TemplateEngine execute template error: %v", err)
}
return nil
}
func (e *TemplateEngine) SetFileHandler(handle FileHandler) {
if handle == nil {
panic("FileHandler can't set nil!")
}
e.fileHandler = handle
}
func (r TemplateRender) Render(w http.ResponseWriter) error {
return r.Engine.executeRender(w, r.Name, r.Data)
}
func (r TemplateRender) WriteContentType(w http.ResponseWriter) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = htmlContentType
}
}
func DefaultFileHandler() FileHandler {
return func(config TemplateConfig, tplFile string) (content string, err error) {
// Get the absolute path of the root template
path, err := filepath.Abs(config.Root + string(os.PathSeparator) + tplFile + config.Extension)
if err != nil {
return "", fmt.Errorf("TemplateEngine path:%v error: %v", path, err)
}
data, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("TemplateEngine render read name:%v, path:%v, error: %v", tplFile, path, err)
}
return string(data), nil
}
}