-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplates.go
208 lines (171 loc) · 4.74 KB
/
templates.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
package got
import (
"bytes"
"fmt"
"html/template"
"net/http"
"path/filepath"
"regexp"
)
// Children define the base template using comments: { /* use basetemplate */ }
var parentRegex = regexp.MustCompile(`\{\s*\/\*\s*use\s(\w+)\s*\*\/\s*\}`)
// Error for loading missing templates
// var ErrNotFound = errors.New("Template not found")
// NotFoundError for type assertions in the caller while still providing context
type NotFoundError struct {
Name string
}
func (t *NotFoundError) Error() string {
return fmt.Sprintf("template %q not found", t.Name)
}
// Might provide a default error template, probably not
// const errorTemplateHTML = `
// <!DOCTYPE html>
// <html>
// <head>
// <meta charset="UTF-8">
// <title>{{.Error}}</title>
// <style type="text/css">
// body { margin: 0 auto; max-width: 600px; }
// pre { background: #eee; padding: 1em; margin: 1em 0; }
// </pre>
// </head>
// <body>
// <h2>Error: {{.Error}}</h2>
// <pre>{{.Trace}}</pre>
// </body>
// </html>`
//
// // Template for displaying errors
// var ErrorTemplate = template.Must(template.New("error").Parse(errorTemplateHTML))
// Templates Collection
type Templates struct {
Extension string
Dir string
Templates map[string]*template.Template
}
// New templates collection
func New(templatesDir, extension string, functions template.FuncMap) (*Templates, error) {
t := &Templates{
Extension: extension,
Dir: templatesDir,
Templates: make(map[string]*template.Template),
}
return t, t.load(functions)
}
// Funcs function map for templates
func (t *Templates) Funcs(functions template.FuncMap) *Templates {
for _, tmpl := range t.Templates {
tmpl.Funcs(functions)
}
return t
}
// Handles loading required templates
func (t *Templates) load(functions template.FuncMap) (err error) {
// Child pages to render
var pages map[string][]byte
pages, err = loadTemplateFiles(t.Dir, "pages/", t.Extension)
if err != nil {
return
}
// Shared templates across multiple pages (sidebars, scripts, footers, etc...)
var includes map[string][]byte
includes, err = loadTemplateFiles(t.Dir, "includes", t.Extension)
if err != nil {
return
}
// Layouts used by pages
var layouts map[string][]byte
layouts, err = loadTemplateFiles(t.Dir, "layouts", t.Extension)
if err != nil {
return
}
var tmpl *template.Template
for name, b := range pages {
matches := parentRegex.FindSubmatch(b)
basename := filepath.Base(name)
tmpl, err = template.New(basename).Funcs(functions).Parse(string(b))
if err != nil {
return
}
// Uses a layout
if len(matches) == 2 {
l, ok := layouts[filepath.Join("layouts", string(matches[1]))]
if !ok {
err = fmt.Errorf("unknown file: layouts/%s%s", matches[1], t.Extension)
return
}
tmpl.New("layout").Parse(string(l))
}
if len(includes) > 0 {
for name, src := range includes {
_, err = tmpl.New(name).Parse(string(src))
if err != nil {
return
}
}
}
t.Templates[basename] = tmpl
}
return
}
// DefinedTemplates loaded by got (for debugging)
func (t *Templates) DefinedTemplates() (out string) {
for _, tmpl := range t.Templates {
out += fmt.Sprintf("%s%s\n", tmpl.Name(), tmpl.DefinedTemplates())
}
return
}
// Render the template & data to the ResponseWriter safely
func (t *Templates) Render(w http.ResponseWriter, template string, data interface{}, status int) error {
buf, err := t.Compile(template, data)
if err != nil {
return err
}
w.WriteHeader(status)
// Set the header and write the buffer to the http.ResponseWriter
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buf.WriteTo(w)
return nil
}
// Compile the template and return the buffer containing the rendered bytes
func (t *Templates) Compile(template string, data interface{}) (*bytes.Buffer, error) {
// Look for the template
tmpl, ok := t.Templates[template]
if !ok {
err := &NotFoundError{template}
return nil, err
}
// Create a buffer so syntax errors don't return a half-rendered response body
var b []byte
buf := bytes.NewBuffer(b)
if err := tmpl.ExecuteTemplate(buf, "layout", data); err != nil {
return buf, err
}
return buf, nil
}
// Not enough benefit for the added complexity
//
// CompileWithBuffer for the template bytes
// func (t *Templates) CompileWithBuffer(template string, data interface{}, buf *bytes.Buffer) error {
//
// // Look for the template
// tmpl, ok := t.Templates[template]
//
// if !ok {
// return &NotFoundError{template}
// }
//
// if err := tmpl.ExecuteTemplate(buf, "layout", data); err != nil {
// return err
// }
//
// return nil
// }
// Make sure any template errors are caught before sending content to client
// A BufferPool will reduce allocs
// var bufpool *bpool.BufferPool
//
// func init() {
// bufpool = bpool.NewBufferPool(32)
// }