/
template.go
75 lines (65 loc) · 1.47 KB
/
template.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
package main
import (
"errors"
"html/template"
"io"
"io/fs"
"path/filepath"
"time"
"github.com/bueti/shrinkster/internal/model"
"github.com/bueti/shrinkster/ui"
"github.com/labstack/echo/v4"
)
type templateData struct {
CurrentYear int
Url *model.Url
Urls []*model.Url
Form any
Flash string
FlashError string
IsAuthenticated bool
CSRFToken string
User *model.User
}
type Template struct {
templates map[string]*template.Template
}
func humanDate(t time.Time) string {
if t.IsZero() {
return ""
}
return t.UTC().Format("02 Jan 2006 at 15:04")
}
var functions = template.FuncMap{
"humanDate": humanDate,
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
tmpl, ok := t.templates[name]
if !ok {
err := errors.New("Template not found -> " + name)
return err
}
return tmpl.ExecuteTemplate(w, "base", data)
}
func (app *application) initTemplate() map[string]*template.Template {
templates := make(map[string]*template.Template)
pages, err := fs.Glob(ui.Files, "html/pages/*.html")
if err != nil {
panic(err)
}
for _, page := range pages {
name := filepath.Base(page)
patterns := []string{
"html/base.tmpl.html",
"html/partials/*.html",
page,
}
// Parse the template
ts, err := template.New(name).Funcs(functions).ParseFS(ui.Files, patterns...)
if err != nil {
panic(err)
}
templates[name] = ts
}
return templates
}