Browse documentation
Documentation

Module parameters

Edit this guide on GitHub

Declares a path parameter and uses it as the module's URL segment.

module_param! does everything path_param! does and also sets the module's segment for module routing. Both macros accept the same options and create the same type for reading values and building links.

For example, module_param!(post_id: u64) gives the module a {post_id} segment. A page in that module can use #[page] without a path.

// src/app/posts/post_id.rs serves /posts/{post_id}.
module_param!(post_id: u64, error = bad_request);

#[page]
async fn post(cx: &Cx) -> Result<impl View> {
    let post_id = path_param::<PostId>(cx)?;
    Ok(view! { "post " (post_id) })
}

Read the value with path_param::<PostId>(cx). To build a link, pass a value such as PostId(42) to href!.

See path_param! for parsing, error handling, visibility, and building URLs.

The name in module_param! sets the parameter name in the URL. In this example, naming the file id.rs would still give the page the path /posts/{post_id}.

Put * before the name to match the rest of the URL path. This catch-all must come last in the route and match at least one segment.

// src/app/docs/doc_path.rs serves /docs/{*doc_path}.
module_param!(*doc_path);

A module adds one URL segment. It can contain one module_param! or one segment!, but not both. To add another parameter, use module_param! in a child module, or use path_param! with a relative path such as #[page("./{comment_id}")].

The module's segment only applies to module routing. It does not change absolute handler paths or paths registered with a regular Router.