/
render.go
113 lines (94 loc) · 2.78 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Render package renders HTML templates found in the rootpath/views folder of the project.
// Go and Jet templating engines can be used. See https://github.com/CloudyKit/jet for full documentation on Jet.
// Expected filenaming for Go templates <template_name>.page.tmpl, for Jet templates <template_name>.jet.
//
// This package has built in CSRF protection with https://github.com/justinas/nosurf.
package render
import (
"errors"
"net/http"
"strings"
"text/template"
"github.com/CloudyKit/jet/v6"
"github.com/justinas/nosurf"
)
type Renderer struct {
rootpath string
templateEngine string
jetViews *jet.Set
}
type TemplateData struct {
IsAuthenticated bool
IntMap map[string]int
StringMap map[string]string
FloatMap map[string]float32
Data map[string]interface{}
CSRFToken string
}
// createRenderer
func New(rootpath, engine string, debug bool) *Renderer {
return &Renderer{
rootpath: rootpath,
templateEngine: engine,
jetViews: makeJetSet(rootpath, debug),
}
}
// Page renders a page with the configured engine
// call the respective methods to render with a specific engine.
func (r *Renderer) Page(w http.ResponseWriter, req *http.Request, view string, variables interface{}, data interface{}) error {
// default template data
td := &TemplateData{}
if data != nil {
td = data.(*TemplateData)
}
// populate default data
td.CSRFToken = nosurf.Token(req)
switch strings.ToLower(r.templateEngine) {
case "go":
return r.GoPage(w, req, view, td)
case "jet":
return r.JetPage(w, req, view, variables, td)
default:
return errors.New("no rendering engine specified")
}
}
// GoPage renders a template using the GO templating engine.
func (r *Renderer) GoPage(w http.ResponseWriter, req *http.Request, view string, data interface{}) error {
tmpl, err := template.ParseFiles(r.rootpath + "/templates/views/" + view + ".page.tmpl")
if err != nil {
return err
}
if err = tmpl.Execute(w, &data); err != nil {
return err
}
return nil
}
// JetPage renders a template using the Jet templating engine.
func (r *Renderer) JetPage(w http.ResponseWriter, req *http.Request, view string, variables interface{}, data interface{}) error {
var vars jet.VarMap
if variables == nil {
vars = make(jet.VarMap)
} else {
vars = variables.(jet.VarMap)
}
t, err := r.jetViews.GetTemplate(view + ".jet")
if err != nil {
return err
}
if err = t.Execute(w, vars, &data); err != nil {
return err
}
return nil
}
// makeJetSet returns a new jet engine
func makeJetSet(rootpath string, debug bool) *jet.Set {
if debug {
return jet.NewSet(
jet.NewOSFileSystemLoader(rootpath+"/templates/views/"),
jet.InDevelopmentMode(),
)
}
return jet.NewSet(
jet.NewOSFileSystemLoader(rootpath + "/templates/views/"),
)
}