The querybind package is a utility library for the Fiber web framework, designed to simplify query parameter binding and state management in HTMX-driven applications. It provides a way to bind query parameters to Go structs and manage URL state seamlessly.
- Integrate more libs/framework, not just fiber
- WithRedirection: might not want to use HX-Push
- WithCustom: might want to do something custom ¯_(ツ)_/¯
- more examples
- Bind URL query parameters to Go structs dynamically using struct tags.
- Maintain and manipulate browser URL state with HTMX requests without full page reloads.
- Provide functional options to customize the behavior of the response binding.
To use the querybind package in your project, run:
go get github.com/davidroman0O/querybindSee examples querybind-example
Bind query parameters from the request to a struct:
type FilterParams struct { Genres []string `querybind:"genres"` Years []int `querybind:"years"` } // ... // return refreshed html of a component app.Get("/page/component", func(c *fiber.Ctx) error { var params FilterParams if err := querybind.Bind(¶ms, c); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) } // Use params here... // return component html })Update the URL state using ResponseBind by setting a HX-Push-Url response header:
// return refreshed html of a component app.Get("/page/component", func(c *fiber.Ctx) error { // Assume params is populated... and you did some processing on some data, whatever querybind.ResponseBind(c, params, querybind.WithPath("/page")) // for the component, you might want to keep the path of the page // Continue with response... that return the html })The ResponseBind function can be customized with the following option:
WithPath(path string): Customizes the path used in theHX-Push-Urlheader.
This project is licensed under the MIT License - see the LICENSE file for details.