Repository navigation
Layout groups for non-module routing #527
|
When you are using the module router functionality, you can declare a module with a underscore prefix (e.g.
However, when not using the module router, I don't see any way to group endpoints that don't share a common path prefix. Is there any way to achieve this manually? |
Replies: 2 comments 1 reply
|
Yep, you can do it by hand. Groups aren't really a module-router thing, they're part of the path syntax: a segment in parentheses like So with the plain router you'd write: #[layout("/(admin)")]
async fn admin_layout(slot: Slot<'_>) -> Result<impl View> {
// ...
}
#[page("/(admin)/users")] // served at /users
async fn users() -> Result<impl View> { /* ... */ }
#[page("/(admin)/settings")] // served at /settings
async fn settings() -> Result<impl View> { /* ... */ }
#[page("/about")] // no admin layout here
async fn about() -> Result<impl View> { /* ... */ }Same goes for layers: put the layer at It's documented on the |
|
Oh, I actually saw that and tried it but it didn't work at first. But now that I try it again, it does seem to work. It must have been user error the first time. Thanks. But the thing I was trying to accomplish is still awkward. Let's say I want a full page layout (i.e. header, footer, main content) for all pages in my app, but on the login page I just want a login form in the middle of the page with no page header / footer. So I could accomplish this with something like: #[layout("/")]
async fn shell(slot: Slot<'_>) -> topcoat::Result<impl View> {
Ok(view! {
<html>
<head>
// shared html head metadata, etc
</head>
<body>
(slot)
</body>
</html>
})
}
#[layout("/(app)")]
async fn app_shell(slot: Slot<'_>) -> topcoat::Result<impl View> {
Ok(view! {
// the full page layout
...
})
}
#[page("/(app)")]
async fn home() -> topcoat::Result<impl View> {
Ok(view! {
"home"
})
}
#[page("/(app)/posts")]
async fn home() -> topcoat::Result<impl View> {
Ok(view! {
"posts"
})
}
#[page("/login")]
async fn foo() -> topcoat::Result<impl View> {
Ok(view! {
"login"
})
}But then I have to prefix every non-authentication endpoint in my app with the |
Yep, you can do it by hand. Groups aren't really a module-router thing, they're part of the path syntax: a segment in parentheses like
(admin)counts for layout/layer matching but gets stripped from the URL. The module router just turns_admininto that under the hood (app::_marketing::pricingbecomes/(_marketing)/pricing).So with the plain router you'd write: