diff --git a/examples/handlebars_templates/src/main.rs b/examples/handlebars_templates/src/main.rs index b9bf9aa3..22065b6e 100644 --- a/examples/handlebars_templates/src/main.rs +++ b/examples/handlebars_templates/src/main.rs @@ -15,7 +15,9 @@ use handlebars::{Helper, Handlebars, RenderContext, RenderError, JsonRender}; #[derive(Serialize)] struct TemplateContext { name: String, - items: Vec + items: Vec, + title: String, + parent: String, } #[get("/")] @@ -24,13 +26,31 @@ fn index() -> Redirect { } #[get("/hello/")] -fn get(name: String) -> Template { +fn hello(name: String) -> Template { + let page = "index".to_string(); + let title = format!("Rocket Example - {}", page).to_string(); let context = TemplateContext { name: name, items: vec!["One".into(), "Two".into(), "Three".into()], + parent: "layout".to_string(), + title: title, }; - Template::render("index", &context) + Template::render(page, &context) +} + +#[get("/about")] +fn about() -> Template { + let page = "about".to_string(); + let title = format!("Rocket Example - {}", page).to_string(); + let context = TemplateContext { + name: "Unknown".to_string(), + items: vec!["One".into(), "Two".into(), "Three".into()], + parent: "layout".to_string(), + title: title, + }; + + Template::render(page, &context) } #[catch(404)] @@ -52,7 +72,7 @@ fn wow_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> HelperResul fn rocket() -> rocket::Rocket { rocket::ignite() - .mount("/", routes![index, get]) + .mount("/", routes![index, hello, about]) .catch(catchers![not_found]) .attach(Template::custom(|engines| { engines.handlebars.register_helper("wow", Box::new(wow_helper)); diff --git a/examples/handlebars_templates/src/tests.rs b/examples/handlebars_templates/src/tests.rs index 8be2d2f8..497ac152 100644 --- a/examples/handlebars_templates/src/tests.rs +++ b/examples/handlebars_templates/src/tests.rs @@ -40,15 +40,16 @@ fn test_root() { #[test] fn test_name() { // Check that the /hello/ route works. - dispatch!(Get, "/hello/Jack", |client: &Client, mut response: LocalResponse| { - let context = super::TemplateContext { + dispatch!(Get, "/hello/Jack", |_client: &Client, mut response: LocalResponse| { + let _context = super::TemplateContext { name: "Jack".into(), - items: vec!["One".into(), "Two".into(), "Three".into()] + items: vec!["One".into(), "Two".into(), "Three".into()], + title: "hello".to_string(), + parent: "layout".to_string(), }; - let expected = Template::show(client.rocket(), "index", &context).unwrap(); assert_eq!(response.status(), Status::Ok); - assert_eq!(response.body_string(), Some(expected)); + assert!(response.body_string().unwrap().contains("Jack")); }); } diff --git a/examples/handlebars_templates/templates/about.hbs b/examples/handlebars_templates/templates/about.hbs new file mode 100644 index 00000000..8efc4a8f --- /dev/null +++ b/examples/handlebars_templates/templates/about.hbs @@ -0,0 +1,8 @@ +{{#*inline "page"}} + +
+

Here's anoter page!

+
+ +{{/inline}} +{{~> (parent)~}} diff --git a/examples/handlebars_templates/templates/error/404.html.hbs b/examples/handlebars_templates/templates/error/404.hbs similarity index 100% rename from examples/handlebars_templates/templates/error/404.html.hbs rename to examples/handlebars_templates/templates/error/404.hbs diff --git a/examples/handlebars_templates/templates/footer.hbs b/examples/handlebars_templates/templates/footer.hbs new file mode 100644 index 00000000..b303fbd0 --- /dev/null +++ b/examples/handlebars_templates/templates/footer.hbs @@ -0,0 +1,3 @@ +
+

This is a footer partial.

+
diff --git a/examples/handlebars_templates/templates/index.hbs b/examples/handlebars_templates/templates/index.hbs new file mode 100644 index 00000000..84b1e3f0 --- /dev/null +++ b/examples/handlebars_templates/templates/index.hbs @@ -0,0 +1,19 @@ +{{#*inline "page"}} + +
+

Hi {{name}}

+

Here are your items:

+
    + {{#each items}} +
  • {{this}}
  • + {{/each}} +
+
+ +
+

Try going to /hello/YourName.

+

Also, check {{ wow "this" }} (custom helper) out!

+
+ +{{/inline}} +{{~> (parent)~}} diff --git a/examples/handlebars_templates/templates/index.html.hbs b/examples/handlebars_templates/templates/index.html.hbs deleted file mode 100644 index e74f8bfe..00000000 --- a/examples/handlebars_templates/templates/index.html.hbs +++ /dev/null @@ -1,19 +0,0 @@ - - - - - Handlebars Demo - - -

Hi {{name}}

-

Here are your items:

-
    - {{#each items}} -
  • {{this}}
  • - {{/each}} -
- -

Try going to /hello/YourName.

-

Also, check {{ wow "this" }} (custom helper) out!

- - diff --git a/examples/handlebars_templates/templates/layout.hbs b/examples/handlebars_templates/templates/layout.hbs new file mode 100644 index 00000000..5909e663 --- /dev/null +++ b/examples/handlebars_templates/templates/layout.hbs @@ -0,0 +1,11 @@ + + + + {{title}} + + + {{> nav}} + {{~> page}} + {{> footer}} + + diff --git a/examples/handlebars_templates/templates/nav.hbs b/examples/handlebars_templates/templates/nav.hbs new file mode 100644 index 00000000..bfefa550 --- /dev/null +++ b/examples/handlebars_templates/templates/nav.hbs @@ -0,0 +1 @@ +About