macro_rules! reverse {
($request:expr, $view_name:literal $(, $($key:ident = $value:expr),*)?) => { ... };
}Expand description
Get a URL for a view by its registered name and given params.
If the view name has two parts separated by a colon, the first part is
considered the app name. If the app name is not provided, the app name of
the request is used. This means that if you don’t specify the app_name,
this macro will only return URLs for views in the same app as the current
request handler.
§Return value
Returns a cot::Result<String> that contains the URL for the view. You
will typically want to append ? to the macro call to get the URL.
§Examples
///
use cot::project::WithConfig;
use cot::request::Request;
use cot::response::{Response, ResponseExt};
use cot::router::{Route, Router};
use cot::{reverse, App, AppBuilder, Body, Project, ProjectContext, StatusCode};
async fn home(request: Request) -> cot::Result<Response> {
// any of below two lines returns the same:
let url = reverse!(request, "home")?;
let url = reverse!(request, "my_custom_app:home")?;
Ok(Response::new_html(
StatusCode::OK,
Body::fixed(format!("Hello! The URL for this view is: {}", url)),
))
}
let router = Router::with_urls([Route::with_handler_and_name("/", home, "home")]);
struct MyApp;
impl App for MyApp {
fn name(&self) -> &'static str {
"my_custom_app"
}
fn router(&self) -> Router {
Router::with_urls([Route::with_handler_and_name("/", home, "home")])
}
}
struct MyProject;
impl Project for MyProject {
fn register_apps(&self, apps: &mut AppBuilder, context: &ProjectContext<WithConfig>) {
apps.register_with_views(MyApp, "");
}
}