https://github.com/mavolin/go-htmx
๐๏ธ High-level wrapper for sending and reading HTMX headers.
https://github.com/mavolin/go-htmx
go golang header headers htmx
Last synced: 9 months ago
JSON representation
๐๏ธ High-level wrapper for sending and reading HTMX headers.
- Host: GitHub
- URL: https://github.com/mavolin/go-htmx
- Owner: mavolin
- License: mit
- Created: 2023-08-21T16:12:24.000Z (over 2 years ago)
- Default Branch: develop
- Last Pushed: 2023-09-03T19:51:44.000Z (over 2 years ago)
- Last Synced: 2025-04-28T22:43:13.242Z (12 months ago)
- Topics: go, golang, header, headers, htmx
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 16
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
go-htmx
[](https://pkg.go.dev/github.com/mavolin/go-htmx)
[](https://goreportcard.com/report/github.com/mavolin/corgi)
[](./LICENSE)
---
## About
go-htmx is a helper library for setting and reading [htmx](https://htmx.org) 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.
```go
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](https://github.com/go-chi/chi), adding the middleware could look like this:
```go
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:
```go
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](https://pkg.go.dev/github.com/mavolin/go-htmx).
## License
Built with โค by [Maximilian von Lindern](https://github.com/mavolin).
Available under the [MIT License](./LICENSE).