https://github.com/davidroman0o/querybind
Fiber utility functions to parse queryparams and preserve its state for web applications
https://github.com/davidroman0o/querybind
fiber htmx queryparameters queryparams
Last synced: 2 months ago
JSON representation
Fiber utility functions to parse queryparams and preserve its state for web applications
- Host: GitHub
- URL: https://github.com/davidroman0o/querybind
- Owner: davidroman0O
- License: mit
- Created: 2023-11-11T21:53:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-16T00:28:48.000Z (over 1 year ago)
- Last Synced: 2025-04-02T20:43:32.278Z (2 months ago)
- Topics: fiber, htmx, queryparameters, queryparams
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# QueryBind
The `querybind` package is a utility library for the [Fiber](https://gofiber.io/) 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.
## TODO
- Integrate more libs/framework, not just fiber
- WithRedirection: might not want to use HX-Push
- WithCustom: might want to do something custom ¯\_(ツ)_/¯
- more examples## Features
- 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.## Installation
To use the `querybind` package in your project, run:
```bash
go get github.com/davidroman0O/querybind
```## Usage
See examples [querybind-example](https://github.com/davidroman0O/querybind-examples)
### QueryBind
Bind query parameters from the request to a struct:
```go
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
})
```### ResponseBind
Update the URL state using `ResponseBind` by setting a `HX-Push-Url` response header:
```go
// 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
})
```## Options
The `ResponseBind` function can be customized with the following option:
- `WithPath(path string)`: Customizes the path used in the `HX-Push-Url` header.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Acknowledgements
- The [Fiber](https://gofiber.io/) team for creating a fantastic web framework.
- The creators and contributors of [HTMX](https://htmx.org/) for enabling modern, dynamic web interactions.