ytdl-web/pkg/views/render.go

29 lines
690 B
Go

package views
import (
"log/slog"
"net/http"
"github.com/a-h/templ"
)
func Handler(component templ.Component) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Render(w, r, component)
}
}
func Render(w http.ResponseWriter, r *http.Request, component templ.Component) {
isHtmx := r.Header.Get("HX-Request") == "true"
if isHtmx {
if err := templ.RenderFragments(r.Context(), w, component, "main-content"); err != nil {
slog.ErrorContext(r.Context(), "failed to render page", slog.Any("error", err))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
component.Render(r.Context(), w)
}