Path Variables
Gothic Framework allows you to create dynamic routes with path parameters using a special naming convention. Any folder or file with the var_ prefix will create a path variable that can be accessed in your middleware function.
For example, if you want to create a route like /users/:userId, you would create a folder named var_userId. The path parameter will then be available through the Chi router URL params.
Here is an example of the folder structure for dynamic routes:
src/pages/
├── users/
│ └── var_userId/
│ └── index.templ → /users/{userId}
└── posts/
└── var_postId/
└── comments/
└── var_commentId.templ
→ /posts/{postId}/comments/{commentId}To access the path variable in your Middleware function, use the chi.URLParam function from the Chi router:
import "github.com/go-chi/chi/v5"
import routes "github.com/gothicframework/core/router"
type UserPageProps struct {
UserId string
}
var UserPageConfig = routes.RouteConfig[UserPageProps]{
Type: routes.DYNAMIC,
HttpMethod: routes.GET,
Middleware: func(w http.ResponseWriter, r *http.Request) UserPageProps {
userId := chi.URLParam(r, "userId")
return UserPageProps{UserId: userId}
},
}Important: The variable name after var_ in your folder/file name must match the parameter name you use in chi.URLParam. For example, var_userId creates a parameter named userId.
Now that you understand file-based routing with path variables, learn how to create Static Pages using Gothic Framework caching configuration!
