htmx-go: Build awesome HTMX projects in Go faster with this new library.
I just made my first HTMX + Go library!
This library provides functions and structs to make working with HTMX + Go faster and easier. Type safety is made possible with enum-like values for setting the HX-Swap header and a convenient way of setting triggers in HX-Trigger with automated JSON encoding.
I noticed that there weren't a lot of Go/HTMX helper modules. awesome-htmx only has an HTML component builder in Go, and the only other HTTP/HTMX library I've found hasn't been updated in a while. I've decided to make my own library that has decent documentation and type safety.
➡️ Documentation: GitHub
Easily check if responses come from HTMX:
func(w http.ResponseWriter, r *http.Request) {
if htmx.IsHTMX(r) {
// logic for handling HTMX requests
} else {
// logic for handling non-HTMX requests (e.g. render a full page for first-time visitors)
}
}
Compose the necessary HTMX response headers with a type-safe, builder syntax:
func(w http.ResponseWriter, r *http.Request) {
htmx.NewResponse().
// Override 'hx-target' to specify which target to load into
Retarget("#errors").
// Also override the 'hx-swap' value of the request
Reswap(htmx.SwapBeforeEnd).
Write(w)
}
If you need to work directly with headers, this library also has string constants for HTMX header names like HX-Boosted, HX-Refresh, and everything else.
You can try out the first pre-release here:
go get github.com/angelofallars/htmx-go
Posted Nov 23, 2023, 10:51 PM UTC
Nice. Just read the readme.md. I'll give it try when I can.
Thanks! I hope you like it. If you want new features, you can open a new issue in the repo here.