-
Notifications
You must be signed in to change notification settings - Fork 1
/
templater.go
42 lines (34 loc) · 965 Bytes
/
templater.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
package main
import (
"gweb"
"html/template"
"io"
"github.com/labstack/echo"
)
// Create a struct to manage templates
type Templater struct {
templates *template.Template
}
// Templater.Render
// Render a template
func (t *Templater) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
// Load Templates
func processTemplates(templater *Templater) {
templater.templates = gweb.LoadTemplatesRecursively("html", ".html")
}
// Setup templater struct
func setupTemplater(e *echo.Echo) {
templater := &Templater{}
templater.templates = gweb.LoadTemplatesRecursively("html", ".html")
e.Renderer = templater
}
// Reload templates, for faster development
func middlewareReprocessTemplates(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
processTemplates(e.Renderer.(*Templater))
next(c)
return nil
}
}