#![feature(proc_macro_hygiene)] #[macro_use] extern crate rocket; #[macro_use] extern crate serde_derive; #[cfg(test)] mod tests; use rocket::Request; use rocket::response::Redirect; use rocket_contrib::templates::{Template, handlebars}; #[derive(Serialize)] struct TemplateContext { title: &'static str, name: Option, items: Vec<&'static str>, // This key tells handlebars which template is the parent. parent: &'static str, } #[get("/")] fn index() -> Redirect { Redirect::to("/hello/Unknown") } #[get("/hello/")] fn hello(name: String) -> Template { Template::render("index", &TemplateContext { title: "Hello", name: Some(name), items: vec!["One", "Two", "Three"], parent: "layout", }) } #[get("/about")] fn about() -> Template { Template::render("about", &TemplateContext { title: "About", name: None, items: vec!["Four", "Five", "Six"], parent: "layout", }) } #[catch(404)] fn not_found(req: &Request<'_>) -> Template { let mut map = std::collections::HashMap::new(); map.insert("path", req.uri().path()); Template::render("error/404", &map) } use self::handlebars::{Helper, Handlebars, Context, RenderContext, Output, HelperResult, JsonRender}; fn wow_helper( h: &Helper<'_, '_>, _: &Handlebars, _: &Context, _: &mut RenderContext<'_, '_>, out: &mut dyn Output ) -> HelperResult { if let Some(param) = h.param(0) { out.write("")?; out.write(¶m.value().render())?; out.write("")?; } Ok(()) } fn rocket() -> rocket::Rocket { rocket::ignite() .mount("/", routes![index, hello, about]) .register(catchers![not_found]) .attach(Template::custom(|engines| { engines.handlebars.register_helper("wow", Box::new(wow_helper)); })) } fn main() { let _ = rocket().launch(); }