go-htmx
go-htmx copied to clipboard
ποΈ High-level wrapper for sending and reading HTMX headers.
About
go-htmx is a helper library for setting and reading htmx headers.
Main Features
- ποΈ High-Level wrapper around the htmx headers
- βοΈ Overwrite header values by calling setters multiple times
- π Fully documentedβso you read the header's docs directly in your IDE
- β Proper JSON handling
Examples
ποΈ Reading Request Headers
The headers set by htmx can be retrieved by calling htmx.Request.
If htmx.Request returns nil htmx.RequestHeaders, the request was not made
by htmx.
func retrieveHeaders(r *http.Request, w http.ResponseWriter) {
fmt.Println("boosted:", htmx.Request(r).Boosted)
fmt.Println("current url:", htmx.Request(r).CurrentURL)
// you get the idea...
}
βοΈ Setting Response Headers
To add response headers, you first need to add the htmx middleware.
By using a middleware instead of setting headers directly you can overwrite response headers at a later point in your code. This is useful if you have a default value for a header that only changes in certain cases.
It also means you can add event triggers one by one and don't have to set them at once.
For chi, adding the middleware could look like this:
r := chi.NewRouter()
r.Use(htmx.NewMiddleware())
The middleware will add the headers once the first call to http.ResponseWriter.Write is made.
After you've added the middleware, you can start setting headers:
type reloadNavData struct {
ActiveEntry string
}
func setHeaders(r *http.Request, w http.ResponseWriter) {
htmx.Retarget(r, "#main")
htmx.Trigger(r, "reload-nav", reloadNavData{ActiveEntry: "foo"})
htmx.Trigger(r, "update-cart", nil)
// HX-Retarget: #main
// HX-Trigger: {"reload-nav": {"ActiveEntry": "foo"}, "update-cart": null}
}
You can find the full list of setters on pkg.go.dev.
License
Built with β€ by Maximilian von Lindern. Available under the MIT License.