summary refs log tree commit diff
path: root/templates.go
blob: 25cb480fd4968012235eb5daef4ea6a71671a65b (plain)
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
package portgate

import (
	"errors"
	"html/template"
	"io"
	"io/fs"
	"path/filepath"
	"strings"
)

// Templates houses all the Portgate page templates and can render them with correct styling.
type Templates struct {
	templateMap map[string]*template.Template
}

// LoadAllTemplates loads all Portgate templates from ./assets/templates/ and bundles them
// into a single Templates instance.
func LoadAllTemplates() (Templates, error) {
	templateMap := make(map[string]*template.Template)

	// We walk through every file in the templates folder.
	err := filepath.Walk("./assets/templates", func(path string, info fs.FileInfo, err error) error {
		// We only care about files which are actual templates and are not of special use (like "_base")
		if !strings.HasPrefix(info.Name(), "_") && strings.HasSuffix(path, ".template.html") {
			// We bundle the templates together with the base template so that we can render them together later.
			t, err := template.ParseFiles("./assets/templates/_base.template.html", path)
			if err == nil {
				// We keep the parsed template in the templateMap by it's filename.
				templateMap[info.Name()] = t
			}
		}

		return err
	})

	if err != nil {
		return Templates{}, err
	}

	return Templates{
		templateMap: templateMap,
	}, nil
}

// ExecuteTemplate executes a single template with the given name and data and writes it to the given
// io.Writer, which is usually fasthttp.RequestCtx.
func (templates *Templates) ExecuteTemplate(w io.Writer, name string, data interface{}) error {
	t := templates.templateMap[name]
	if t == nil {
		return errors.New("Unknown template name: " + name)
	}

	return t.ExecuteTemplate(w, "_base.template.html", data)
}