Skip to main content

ViewData

Trait ViewData 

Source
pub trait ViewData: Serialize {
    const TEMPLATE: &'static str;
    const FIELDS: &'static [&'static str];
}
Expand description

The data of one page: a struct whose fields are its template’s variables, so the compiler checks the handler’s side and view:check the template’s (the variables it uses that the struct lacks). Derive it with #[derive(ViewData)] and #[view(template = "…")], render it with View::typed, and register it with App::view_data::<T>() (Registry::view_data in a module) so view:check knows it.

#[derive(serde::Serialize, ViewData)]
#[view(template = "suppliers/index.html")]
struct SupplierList {
    suppliers: Vec<String>,
    #[serde(rename = "total")]
    count: usize,
}

assert_eq!(SupplierList::TEMPLATE, "suppliers/index.html");
assert_eq!(SupplierList::FIELDS, ["suppliers", "total"]);

async fn index() -> View {
    View::typed(SupplierList { suppliers: vec!["Velo".into()], count: 1 })
}
let app = App::new().view_data::<SupplierList>();

The check reads what MiniJinja loads (after the rx-* components are compiled) and follows neither extends nor include nor imported macros: a variable used only in the layout or a partial isn’t checked. Besides the struct’s fields, a template may use the request globals (auth, request, t, old…), the environment’s functions and globals, the App::share keys and the names it defines itself.

Required Associated Constants§

Source

const TEMPLATE: &'static str

The template the page renders, relative to VIEWS_PATH.

Source

const FIELDS: &'static [&'static str]

The names the template sees: the fields as serialized (#[serde(rename)] applied, skipped fields left out).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§