-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_engine.go
196 lines (162 loc) · 4.83 KB
/
go_engine.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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// aahframework.org/view source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package view
import (
"bytes"
"html/template"
"path"
"path/filepath"
"strings"
"sync"
"aahframework.org/config.v0"
"aahframework.org/log.v0"
"aahframework.org/vfs.v0"
)
const noLayout = "nolayout"
var (
commonTemplates *Templates
bufPool *sync.Pool
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// type GoViewEngine and its method
//______________________________________________________________________________
// GoViewEngine implements the partial inheritance support with Go templates.
type GoViewEngine struct {
*EngineBase
}
// Init method initialize a template engine with given aah application config
// and application views base path.
func (e *GoViewEngine) Init(fs *vfs.VFS, appCfg *config.Config, baseDir string) error {
if e.EngineBase == nil {
e.EngineBase = new(EngineBase)
}
if err := e.EngineBase.Init(fs, appCfg, baseDir, "go", ".html"); err != nil {
return err
}
// Add template func
AddTemplateFunc(template.FuncMap{
"safeHTML": e.tmplSafeHTML,
"import": e.tmplInclude,
"include": e.tmplInclude, // alias for import
})
// load common templates
if err := e.loadCommonTemplates(); err != nil {
return err
}
// collect all layouts
layouts, err := e.LayoutFiles()
if err != nil {
return err
}
// load layout templates
if err = e.loadLayoutTemplates(layouts); err != nil {
return err
}
if !e.IsLayoutEnabled {
// since pages directory processed above, no error expected here
_ = e.loadNonLayoutTemplates("pages")
}
if e.VFS.IsExists(filepath.Join(e.BaseDir, "errors")) {
if err = e.loadNonLayoutTemplates("errors"); err != nil {
return err
}
}
return nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// GoViewEngine unexported methods
//______________________________________________________________________________
func (e *GoViewEngine) loadCommonTemplates() error {
commons, err := e.FilesPath("common")
if err != nil {
return err
}
commonTemplates = &Templates{}
bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
prefix := path.Dir(e.BaseDir)
for _, file := range commons {
if !strings.HasSuffix(file, e.FileExt) {
log.Warnf("goviewengine: not a valid template extension[%s]: %s", e.FileExt, TrimPathPrefix(prefix, file))
continue
}
log.Tracef("Parsing file: %s", TrimPathPrefix(prefix, file))
tmpl, err := e.ParseFile(file)
if err != nil {
return err
}
if err = commonTemplates.Add(tmpl.Name(), tmpl); err != nil {
return err
}
}
return nil
}
func (e *GoViewEngine) loadLayoutTemplates(layouts []string) error {
dirs, err := e.DirsPath("pages")
if err != nil {
return err
}
prefix := path.Dir(e.BaseDir)
var errs []error
for _, layout := range layouts {
layoutKey := strings.ToLower(path.Base(layout))
for _, dir := range dirs {
files, err := e.VFS.Glob(path.Join(dir, "*"+e.FileExt))
if err != nil {
errs = append(errs, err)
continue
}
for _, file := range files {
tmplKey := StripPathPrefixAt(filepath.ToSlash(file), "views/")
tmpl := e.NewTemplate(tmplKey)
tfiles := []string{layout, file}
log.Tracef("Parsing files: %s", TrimPathPrefix(prefix, tfiles...))
if _, err = e.ParseFiles(tmpl, tfiles...); err != nil {
errs = append(errs, err)
continue
}
if err = e.AddTemplate(layoutKey, tmplKey, tmpl); err != nil {
errs = append(errs, err)
continue
}
}
}
}
return e.ParseErrors(errs)
}
func (e *GoViewEngine) loadNonLayoutTemplates(scope string) error {
dirs, err := e.DirsPath(scope)
if err != nil {
return err
}
prefix := path.Dir(e.BaseDir)
var errs []error
for _, dir := range dirs {
files, err := e.VFS.Glob(path.Join(dir, "*"+e.FileExt))
if err != nil {
errs = append(errs, err)
continue
}
for _, file := range files {
tmplKey := noLayout + "-" + StripPathPrefixAt(filepath.ToSlash(file), "views/")
tmpl := e.NewTemplate(tmplKey)
log.Tracef("Parsing file: %s", TrimPathPrefix(prefix, file))
tstr, err := e.Open(file)
if err != nil {
return err
}
if tmpl, err = tmpl.Parse(tstr); err != nil {
errs = append(errs, err)
continue
}
if err = e.AddTemplate(noLayout, tmplKey, tmpl); err != nil {
errs = append(errs, err)
continue
}
}
}
return e.ParseErrors(errs)
}
func init() {
_ = AddEngine("go", &GoViewEngine{})
}