Macros
context_update!, context!, impl_from_error!
● ● ●
pub async fn my_view(mut request: Request) -> AppResult<Response> {
let articles = vec!["Article 1", "Article 2"];
// Inject into Tera context
context_update!(request => {
"title" => "My page",
"articles" => &articles,
"count" => articles.len(),
});
request.render("my_page.html")
}
// In a handler
success!(request.notices => "Saved successfully!");
error!(request.notices => "An error occurred.");
info!(request.notices => "Checking...");
warning!(request.notices => "Warning: session expiring soon.");
// flash_now! — message injected directly into context
context_update!(request => {
"messages" => flash_now!(success => "Created!"),
});
// `?` works on DbErr and anyhow::Error out of the box:
// Runique converts them into AppError (impl_from_error! in the framework).
pub async fn handler(mut request: Request) -> AppResult<Response> {
let article = Article::find_by_id(1)
.one(&request.engine.db)
.await?; // DbErr -> AppError
context_update!(request => { "article" => &article });
request.render("page.html")
}
// Your own error: build the AppError explicitly
return Err(Box::new(AppError::new(ErrorContext::not_found("Article not found"))));