peachy-go/desktop/routes/util.go

28 lines
655 B
Go

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)
}
}