summaryrefslogtreecommitdiffstats
path: root/cmd/web/templates.go
blob: 2457053ba71b520957021bce08cf954e84730d64 (plain) (blame)
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
package main

import (
	"html/template"
	"path/filepath"

	"github.com/yulqen/ded-go-core/internal/models"
)

type templateData struct {
	Operations []models.Operation
}

func newTemplateCache() (map[string]*template.Template, error) {
	cache := map[string]*template.Template{}

	// get a slice of filepaths match the pattern for our page templates
	// e.g. [ui/html/pages/home.tmpl.html ui/html/pages/view.tmpl.html]
	pages, err := filepath.Glob("./ui/html/pages/*.html")
	if err != nil {
		return nil, err
	}

	// loop through the page filepaths one by one
	for _, page := range pages {
		// extract "home.tmp.html" from the full filepath.
		name := filepath.Base(page)

		files := []string{
			"./ui/html/base.tmpl.html",
			"./ui/html/partials/nav.tmpl.html",
			page,
		}

		ts, err := template.ParseFiles(files...)
		if err != nil {
			return nil, err
		}

		cache[name] = ts
	}

	return cache, nil
}