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::response::Redirect;
use rocket_contrib::{Template, handlebars};
use handlebars::{Helper, Handlebars, RenderContext, RenderError, JsonRender};
#[derive(Serialize)]
struct TemplateContext {
name: String,
items: Vec<String>,
title: String,
parent: String,
title: &'static str,
name: Option<String>,
items: Vec<&'static str>,
// This key tells handlebars which template is the parent.
parent: &'static str,
}
#[get("/")]
@ -27,30 +29,22 @@ fn index() -> Redirect {
#[get("/hello/<name>")]
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(page, &context)
Template::render("index", &TemplateContext {
title: "Hello",
name: Some(name),
items: vec!["One", "Two", "Three"],
parent: "layout",
})
}
#[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)
Template::render("about", &TemplateContext {
title: "About",
name: None,
items: vec!["Four", "Five", "Six"],
parent: "layout",
})
}
#[catch(404)]
@ -60,9 +54,7 @@ fn not_found(req: &Request) -> Template {
Template::render("error/404", &map)
}
type HelperResult = Result<(), RenderError>;
fn wow_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> HelperResult {
fn wow_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
if let Some(param) = h.param(0) {
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::http::Method::*;
use rocket::http::Status;
@ -40,16 +41,17 @@ fn test_root() {
#[test]
fn test_name() {
// Check that the /hello/<name> route works.
dispatch!(Get, "/hello/Jack", |_client: &Client, mut response: LocalResponse| {
let _context = super::TemplateContext {
name: "Jack".into(),
items: vec!["One".into(), "Two".into(), "Three".into()],
title: "hello".to_string(),
parent: "layout".to_string(),
dispatch!(Get, "/hello/Jack%20Daniels", |client: &Client, mut response: LocalResponse| {
let context = TemplateContext {
title: "Hello",
name: Some("Jack Daniels".into()),
items: vec!["One", "Two", "Three"],
parent: "layout",
};
let expected = Template::show(client.rocket(), "index", &context).unwrap();
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"}}
<section id="about">
<h1>Here's anoter page!</h1>
<h1>Here's another page!</h1>
<ul>
{{#each items}}
<li>{{ this }}</li>
{{/each}}
</ul>
</section>
{{/inline}}

View File

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

View File

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

View File

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