aboutsummaryrefslogtreecommitdiff
path: root/static.go
blob: 1c6a086b026205651a0d837e10588b65c5df8aaf (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
package img

import (
	"embed"
	"fmt"
	"html/template"
	"io"
)

var (
	//go:embed templates/*.html
	TemplateFS embed.FS

	//go:embed static/*
	StaticFS embed.FS

	Template *template.Template
)

type HTMLView[T any] struct {
	Title    string
	Username string
	Data     T
}

func Render[T any](w io.Writer, page string, view *HTMLView[T]) error {
	pageFile := fmt.Sprintf("templates/%s", page)
	tmpl, err := template.New("").ParseFS(TemplateFS, "templates/layout.html", pageFile)
	if err != nil {
		return err
	}

	return tmpl.ExecuteTemplate(w, page, view)
}