/
ttpl.go
262 lines (238 loc) · 6.08 KB
/
ttpl.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package ttpl
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"text/template"
"time"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
)
type (
// PageTemplate struct for gin
PageTemplate struct {
TemplatePath string
templates *template.Template
funcMap template.FuncMap
}
// PageRender struct for gin
PageRender struct {
Template *template.Template
funcMap template.FuncMap
Data interface{}
Name string
}
)
var (
dmu sync.Mutex
dots = map[string]string{}
spliter = regexp.MustCompile("[\\s\\/]+")
siteVars = map[string]bool{
"action": false,
"title": false,
"login": false,
"module": false,
"path": false,
"ajax": false,
"socket": false,
"allow": false,
"js": true,
"css": true,
"lang": false,
"par_0": false,
"par_1": false,
"par_2": false,
"par_3": false,
}
)
func init() {
if os.Getenv("ENV") == "development" {
go func() {
for range time.Tick(time.Second) {
func() {
dmu.Lock()
defer dmu.Unlock()
dots = map[string]string{}
}()
}
}()
}
}
func (r PageTemplate) Instance(name string, data interface{}) render.Render {
return PageRender{
Template: r.templates,
funcMap: r.funcMap,
Name: name,
Data: data,
}
}
func (r PageRender) WriteContentType(w http.ResponseWriter) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = []string{"text/html; charset=utf-8"}
}
}
func (r PageRender) Render(w http.ResponseWriter) error {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = []string{"text/html; charset=utf-8"}
}
site := map[string]interface{}{}
for key, array := range siteVars {
Key := strings.Title(key)
if array {
site[key] = header[Key]
} else {
site[key] = ""
if val := header[Key]; len(val) > 0 {
site[key] = val[0]
}
}
delete(header, Key)
}
r.Template.Funcs(template.FuncMap{
"site": func(name string) interface{} { return site[name] },
"o": func(key string) interface{} {
if lang, ok := site["lang"]; ok {
if translate, ok1 := r.funcMap["translate"]; ok1 {
if translate2, ok2 := translate.(func(module, key, lang string) string); ok2 {
return translate2(site["module"].(string), key, lang.(string))
}
}
}
return ""
},
})
name := r.Name
if name != "login.html" && name != "firstStart.html" && site["allow"] != "true" {
name = "access-deny"
}
if len(name) > 0 {
if err := r.Template.ExecuteTemplate(w, name, r.Data); err != nil {
fmt.Println("Template err: ", err.Error())
}
} else {
if err := r.Template.Execute(w, r.Data); err != nil {
fmt.Println("Template err: ", err.Error())
}
}
return nil
}
func dot(dotPath string) func(name string) string {
return func(name string) string {
dmu.Lock()
defer dmu.Unlock()
fullname := dotPath + "/" + name
if _, exists := dots[fullname]; !exists {
dots[fullname] = "<!-- Template '" + name + "' not found! -->\n"
if dat, err := ioutil.ReadFile(fullname); err == nil {
s := strings.Split(name, ".")
tplName := spliter.Split(s[0], -1)
if s[len(s)-1] == "js" { // js темплейты
dots[fullname] = "<!-- doT.js template - " + name + " -->\n" +
"<script type='text/javascript' id='tpl_" + strings.Join(tplName[1:], "-") + "'>\n" + string(dat) + "</script>\n"
} else { // html темплейты
dots[fullname] = "<!-- doT.js template - " + name + " -->\n" +
"<script type='text/html' id='tpl_" + strings.Join(tplName[1:], "-") + "'>\n" + string(dat) + "</script>\n"
}
}
}
return dots[fullname]
}
}
// Use ttpl render
func Use(r *gin.Engine, pathes []string, dotPath string, funcMap ...template.FuncMap) {
t := template.New("")
if len(funcMap) == 0 {
funcMap = []template.FuncMap{}
}
funcMap[0]["dot"] = dot(dotPath)
t = t.Funcs(funcMap[0])
for _, path := range pathes {
if _, err := os.Stat(path); err == nil {
filepath.Walk(path, func(file string, info os.FileInfo, err error) error {
if !info.IsDir() && err == nil {
tt, err := parseFiles(t, dotPath, path, file)
if err == nil {
t = tt
} else if err != nil {
fmt.Println(err)
}
}
return nil
})
}
}
r.HTMLRender = PageTemplate{"/", t, funcMap[0]}
}
// parseFiles is the helper for the method and function. If the argument
// template is nil, it is created from the first file.
func parseFiles(t *template.Template, dotPath string, path string, filenames ...string) (*template.Template, error) {
for _, filename := range filenames {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
s := string(b)
name := strings.Replace(filename, path, "", 1)
shortName := strings.Split(name, ".")[0]
// DoT.js
dots, err := filepath.Glob(dotPath + "/" + shortName + "/*")
if len(dots) > 0 {
for _, dot := range dots {
base := filepath.Base(dot)
if !strings.Contains(base, ".") {
subs, _ := filepath.Glob(dotPath + "/" + shortName + "/" + base + "/*")
for _, sub := range subs {
if !strings.Contains(filepath.Base(sub), " ") {
s = s + `{{ dot "` + shortName + `/` + base + `/` + filepath.Base(sub) + `"}}` + "\n"
}
}
} else if !strings.Contains(base, " ") {
s = s + `{{ dot "` + shortName + `/` + base + `" }}` + "\n"
}
}
}
sOrigin := s
header := `{{template "header" .}}`
footer := `{{template "footer" .}}`
if strings.Contains(s, "SUMMER-NO-HEADER") {
header = ""
}
if strings.Contains(s, "SUMMER-NO-FOOTER") {
footer = ""
}
if name != "layout.html" && name != "login.html" && name != "firstStart.html" {
s = header + s + footer
}
var tmpl *template.Template
if t == nil {
t = template.New(name)
}
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(s)
if err != nil {
return nil, err
}
name = "summer-origin-" + name
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(sOrigin)
if err != nil {
return nil, err
}
}
return t, nil
}