templates: Share fragments

This commit is contained in:
Tom Wiesing 2022-10-20 11:26:35 +02:00
parent dd7be3f520
commit d64e6f8ec3
No known key found for this signature in database
12 changed files with 111 additions and 93 deletions

View file

@ -6,8 +6,10 @@ import (
)
type HTMLHandler[T any] struct {
Handler func(r *http.Request) (T, error)
Template *template.Template // called with T
Handler func(r *http.Request) (T, error)
Template *template.Template // called with T
TemplateName string // name of template to render, defaults to root
}
var htmlInternalServerErr = []byte(`<!DOCTYPE HTML><title>Internal Server Error</title>Internal Server Error`)
@ -37,5 +39,10 @@ func (h HTMLHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// write out the response as json
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
h.Template.Execute(w, result)
if h.TemplateName != "" {
h.Template.ExecuteTemplate(w, h.TemplateName, result)
} else {
h.Template.Execute(w, result)
}
}