Tidy up handlebars example.

This commit is contained in:
Sergio Benitez 2018-07-02 14:11:09 -07:00
parent f00c68252c
commit 94cfba8b49
6 changed files with 39 additions and 40 deletions

View File

@ -10,14 +10,16 @@ extern crate rocket;
use rocket::Request; use rocket::Request;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket_contrib::{Template, handlebars}; use rocket_contrib::{Template, handlebars};
use handlebars::{Helper, Handlebars, RenderContext, RenderError, JsonRender}; use handlebars::{Helper, Handlebars, RenderContext, RenderError, JsonRender};
#[derive(Serialize)] #[derive(Serialize)]
struct TemplateContext { struct TemplateContext {
name: String, title: &'static str,
items: Vec<String>, name: Option<String>,
title: String, items: Vec<&'static str>,
parent: String, // This key tells handlebars which template is the parent.
parent: &'static str,
} }
#[get("/")] #[get("/")]
@ -27,30 +29,22 @@ fn index() -> Redirect {
#[get("/hello/<name>")] #[get("/hello/<name>")]
fn hello(name: String) -> Template { fn hello(name: String) -> Template {
let page = "index".to_string(); Template::render("index", &TemplateContext {
let title = format!("Rocket Example - {}", page).to_string(); title: "Hello",
let context = TemplateContext { name: Some(name),
name: name, items: vec!["One", "Two", "Three"],
items: vec!["One".into(), "Two".into(), "Three".into()], parent: "layout",
parent: "layout".to_string(), })
title: title,
};
Template::render(page, &context)
} }
#[get("/about")] #[get("/about")]
fn about() -> Template { fn about() -> Template {
let page = "about".to_string(); Template::render("about", &TemplateContext {
let title = format!("Rocket Example - {}", page).to_string(); title: "About",
let context = TemplateContext { name: None,
name: "Unknown".to_string(), items: vec!["Four", "Five", "Six"],
items: vec!["One".into(), "Two".into(), "Three".into()], parent: "layout",
parent: "layout".to_string(), })
title: title,
};
Template::render(page, &context)
} }
#[catch(404)] #[catch(404)]
@ -60,9 +54,7 @@ fn not_found(req: &Request) -> Template {
Template::render("error/404", &map) Template::render("error/404", &map)
} }
type HelperResult = Result<(), RenderError>; fn wow_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
fn wow_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> HelperResult {
if let Some(param) = h.param(0) { if let Some(param) = h.param(0) {
write!(rc.writer, "<b><i>{}</i></b>", param.value().render())?; write!(rc.writer, "<b><i>{}</i></b>", param.value().render())?;
} }

View File

@ -1,4 +1,5 @@
use super::rocket; use super::{rocket, TemplateContext};
use rocket::local::{Client, LocalResponse}; use rocket::local::{Client, LocalResponse};
use rocket::http::Method::*; use rocket::http::Method::*;
use rocket::http::Status; use rocket::http::Status;
@ -40,16 +41,17 @@ fn test_root() {
#[test] #[test]
fn test_name() { fn test_name() {
// Check that the /hello/<name> route works. // Check that the /hello/<name> route works.
dispatch!(Get, "/hello/Jack", |_client: &Client, mut response: LocalResponse| { dispatch!(Get, "/hello/Jack%20Daniels", |client: &Client, mut response: LocalResponse| {
let _context = super::TemplateContext { let context = TemplateContext {
name: "Jack".into(), title: "Hello",
items: vec!["One".into(), "Two".into(), "Three".into()], name: Some("Jack Daniels".into()),
title: "hello".to_string(), items: vec!["One", "Two", "Three"],
parent: "layout".to_string(), parent: "layout",
}; };
let expected = Template::show(client.rocket(), "index", &context).unwrap();
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
assert!(response.body_string().unwrap().contains("Jack")); assert_eq!(response.body_string(), Some(expected));
}); });
} }

View File

@ -1,7 +1,12 @@
{{#*inline "page"}} {{#*inline "page"}}
<section id="about"> <section id="about">
<h1>Here's anoter page!</h1> <h1>Here's another page!</h1>
<ul>
{{#each items}}
<li>{{ this }}</li>
{{/each}}
</ul>
</section> </section>
{{/inline}} {{/inline}}

View File

@ -1,11 +1,11 @@
{{#*inline "page"}} {{#*inline "page"}}
<section id="hello"> <section id="hello">
<h1>Hi {{name}}</h1> <h1>Hi {{ name }}!</h1>
<h3>Here are your items:</h3> <h3>Here are your items:</h3>
<ul> <ul>
{{#each items}} {{#each items}}
<li>{{this}}</li> <li>{{ this }}</li>
{{/each}} {{/each}}
</ul> </ul>
</section> </section>

View File

@ -1,7 +1,7 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
<title>{{title}}</title> <title>Rocket Example - {{ title }}</title>
</head> </head>
<body> <body>
{{> nav}} {{> nav}}

View File

@ -1 +1 @@
<a href="/about">About</a> <a href="/hello/Unknown">Hello</a> | <a href="/about">About</a>