https://github.com/jackielii/structpages
Build Templ pages with Struct-based Routing
https://github.com/jackielii/structpages
go htmx routing templ urlfor
Last synced: about 2 months ago
JSON representation
Build Templ pages with Struct-based Routing
- Host: GitHub
- URL: https://github.com/jackielii/structpages
- Owner: jackielii
- License: bsd-3-clause
- Created: 2025-05-30T10:17:41.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-11-04T16:10:07.000Z (8 months ago)
- Last Synced: 2025-11-04T18:10:19.320Z (8 months ago)
- Topics: go, htmx, routing, templ, urlfor
- Language: Go
- Homepage:
- Size: 14.8 MB
- Stars: 8
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Support: docs/supported-flows.md
Awesome Lists containing this project
README
# structpages
[](https://github.com/jackielii/structpages/actions/workflows/ci.yml)
[](https://pkg.go.dev/github.com/jackielii/structpages)
[](https://codecov.io/gh/jackielii/structpages)
[](https://goreportcard.com/report/github.com/jackielii/structpages)
[](https://opensource.org/licenses/MIT)
Struct Pages provides a way to define routing using struct tags and methods. It integrates with Go's `http.ServeMux`, allowing you to quickly build web applications with minimal boilerplate.
**Status**: **Alpha** - This package is in early development and may have breaking changes in the future. Currently used in a medium-sized project, but not yet battle-tested in production.
## Features
- 🏗️ **Struct-based routing** - Define routes using struct tags
- 🎨 **Templ support** - Built-in integration with [Templ](https://templ.guide/)
- ⚡ **HTMX-friendly** - Automatic partial rendering support
- 🔧 **Middleware** - Standard Go middleware pattern
- 🎯 **Type-safe URLs** - Generate URLs from struct references
- 📦 **Dependency injection** - Pass dependencies to handlers via options
## Installation
```shell
go get github.com/jackielii/structpages
```
## Quick Start
Define your page structure using struct tags:
```go
package main
import (
"log"
"net/http"
"github.com/jackielii/structpages"
)
type index struct {
product `route:"/product Product"`
team `route:"/team Team"`
contact `route:"/contact Contact"`
}
// Implement the Page method using Templ
templ (index) Page() {
Welcome
Product
Team
Contact
}
func main() {
mux := http.NewServeMux()
_, err := structpages.Mount(mux, index{}, "/", "Home")
if err != nil {
log.Fatal(err)
}
log.Println("Starting server on :8080")
http.ListenAndServe(":8080", mux)
}
```
Route definitions use the format `[method] path [Title]`:
- `/path` - All methods, no title
- `POST /path` - POST requests only
- `GET /path Page Title` - GET requests with title "Page Title"
## Documentation
- [API Reference](./docs/api.md) - Complete API documentation for Mount, options, and methods
- [Routing Patterns](./docs/routing.md) - Route definitions, path parameters, nested routes
- [Supported Request Flows](./docs/supported-flows.md) - How requests are dispatched to handlers and components
- [Middleware](./docs/middleware.md) - Using middleware with your pages
- [HTMX Integration](./docs/htmx.md) - Partial rendering and HTMX support
- [URLFor & ID Generation](./docs/urlfor.md) - Type-safe URL and HTML id generation
- [Templ Patterns](./docs/templ.md) - Working with Templ templates
- [Advanced Features](./docs/advanced.md) - Dependency injection, Init, dynamic Refs, type aliases
## Examples
Check out the [examples directory](./examples) for complete working applications:
- [Simple](./examples/simple) - Basic routing and page rendering
- [HTMX](./examples/htmx) - HTMX integration with partial updates
- [HTMX RenderTarget](./examples/htmx-render-target) - Standalone-function components shared across pages, with per-component data loading via `RenderTarget`
- [Todo](./examples/todo) - Full TODO app: form actions via `ServeHTTP` returning `RenderComponent(...)` to re-render a sibling component (in-memory store)
## Contributing
See [CONTRIBUTING.md](./CONTRIBUTING.md) for development setup and guidelines.
## License
MIT License - see [LICENSE](./LICENSE) file for details.