-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
91 lines (74 loc) · 1.99 KB
/
render.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
package render
import (
"bytes"
"errors"
"fmt"
"github.com/bogdanrat/web-server/contracts/models"
"github.com/bogdanrat/web-server/service/core/config"
"github.com/bogdanrat/web-server/service/core/lib"
"html/template"
"log"
"net/http"
"path/filepath"
)
const (
pathToTemplates = "./templates"
)
var (
functions = template.FuncMap{
"formatSize": lib.FormatSize,
}
)
func Template(w http.ResponseWriter, r *http.Request, tmpl string, templateData *models.TemplateData) error {
var templateCache map[string]*template.Template
var err error
if !config.AppConfig.Server.DevelopmentMode {
templateCache = config.AppConfig.TemplateCache
} else {
templateCache, err = CreateTemplateCache()
if err != nil {
log.Println("cannot create template cache:", err.Error())
return errors.New(fmt.Sprintf("cannot create template cache: %s", err.Error()))
}
}
t, ok := templateCache[tmpl]
if !ok {
log.Println("template not found in cache")
return errors.New("template not found in cache")
}
buf := new(bytes.Buffer)
_ = t.Execute(buf, templateData)
_, err = buf.WriteTo(w)
if err != nil {
log.Println("error writing template: ", err)
return errors.New(fmt.Sprintf("error writing template: %s", err.Error()))
}
w.WriteHeader(http.StatusOK)
return nil
}
func CreateTemplateCache() (map[string]*template.Template, error) {
templateCache := map[string]*template.Template{}
pages, err := filepath.Glob(fmt.Sprintf("%s/*.page.tmpl", pathToTemplates))
if err != nil {
return nil, err
}
for _, page := range pages {
name := filepath.Base(page)
ts, err := template.New(name).Funcs(functions).ParseFiles(page)
if err != nil {
return nil, err
}
matches, err := filepath.Glob(fmt.Sprintf("%s/*.layout.tmpl", pathToTemplates))
if err != nil {
return nil, err
}
if len(matches) > 0 {
ts, err = ts.ParseGlob(fmt.Sprintf("%s/*.layout.tmpl", pathToTemplates))
if err != nil {
return nil, err
}
}
templateCache[name] = ts
}
return templateCache, nil
}