Use inheritance in handlebars example.

This commit is contained in:
Ashley Williams 2018-03-24 16:43:06 +01:00 committed by Sergio Benitez
parent a383d49ab0
commit f00c68252c
9 changed files with 72 additions and 28 deletions

View File

@ -15,7 +15,9 @@ use handlebars::{Helper, Handlebars, RenderContext, RenderError, JsonRender};
#[derive(Serialize)]
struct TemplateContext {
name: String,
items: Vec<String>
items: Vec<String>,
title: String,
parent: String,
}
#[get("/")]
@ -24,13 +26,31 @@ fn index() -> Redirect {
}
#[get("/hello/<name>")]
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));

View File

@ -40,15 +40,16 @@ 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 {
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"));
});
}

View File

@ -0,0 +1,8 @@
{{#*inline "page"}}
<section id="about">
<h1>Here's anoter page!</h1>
</section>
{{/inline}}
{{~> (parent)~}}

View File

@ -0,0 +1,3 @@
<footer>
<p>This is a footer partial.</p>
</footer>

View File

@ -0,0 +1,19 @@
{{#*inline "page"}}
<section id="hello">
<h1>Hi {{name}}</h1>
<h3>Here are your items:</h3>
<ul>
{{#each items}}
<li>{{this}}</li>
{{/each}}
</ul>
</section>
<section id="custom-helper">
<p>Try going to <a href="/hello/YourName">/hello/YourName</a>.</p>
<p>Also, check {{ wow "this" }} (custom helper) out!</p>
</section>
{{/inline}}
{{~> (parent)~}}

View File

@ -1,19 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Handlebars Demo</title>
</head>
<body>
<h1>Hi {{name}}</h1>
<h3>Here are your items:</h3>
<ul>
{{#each items}}
<li>{{this}}</li>
{{/each}}
</ul>
<p>Try going to <a href="/hello/YourName">/hello/YourName</a>.</p>
<p>Also, check {{ wow "this" }} (custom helper) out!</p>
</body>
</html>

View File

@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
{{> nav}}
{{~> page}}
{{> footer}}
</body>
</html>

View File

@ -0,0 +1 @@
<a href="/about">About</a>