templates: Share fragments
This commit is contained in:
parent
dd7be3f520
commit
d64e6f8ec3
12 changed files with 111 additions and 93 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue