peachy-go/desktop/routes/util.go

28 lines
655 B
Go
Raw Permalink Normal View History

2024-11-05 12:17:27 +00:00
package routes
import (
"net/http"
"git.codemonkeysoftware.net/b/hatmill"
"git.codemonkeysoftware.net/b/peachy-go/desktop/pages"
"github.com/mavolin/go-htmx"
)
/*
Render the component, and if it's an HX request, only render the component.
*/
func HXRender[T hatmill.Term](w http.ResponseWriter, r *http.Request, component func() T) {
hxRequest := htmx.Request(r)
w.Header().Set("Content-Type", "text/html")
// If it's an HX request, we only render the component.
// If it's not, we render the whole page.
if hxRequest == nil {
hatmill.WriteDocument(w, pages.Page(component))
pages.Page(component)
} else {
component().WriteTo(w)
}
}