summaryrefslogtreecommitdiff
path: root/pkg/webui/render.go
blob: 23f5e5140d08ed74ab193a07f9b5b34c49e027d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package webui

import (
	"bytes"
	"fmt"
	"html/template"
	"net/http"
)

func render(w http.ResponseWriter, t *template.Template, status int, data any) {
	var buf bytes.Buffer
	if err := t.ExecuteTemplate(&buf, "base.html", data); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		_, _ = w.Write([]byte(fmt.Sprintf(
			"%s: failed to execute template: %+v",
			http.StatusText(http.StatusInternalServerError),
			err)))
	} else {
		w.WriteHeader(status)
		_, _ = buf.WriteTo(w)
	}
}