mirror of https://github.com/rwf2/Rocket.git
Use inheritance in handlebars example.
This commit is contained in:
parent
a383d49ab0
commit
f00c68252c
|
@ -15,7 +15,9 @@ use handlebars::{Helper, Handlebars, RenderContext, RenderError, JsonRender};
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct TemplateContext {
|
struct TemplateContext {
|
||||||
name: String,
|
name: String,
|
||||||
items: Vec<String>
|
items: Vec<String>,
|
||||||
|
title: String,
|
||||||
|
parent: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
@ -24,13 +26,31 @@ fn index() -> Redirect {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/hello/<name>")]
|
#[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 {
|
let context = TemplateContext {
|
||||||
name: name,
|
name: name,
|
||||||
items: vec!["One".into(), "Two".into(), "Three".into()],
|
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)]
|
#[catch(404)]
|
||||||
|
@ -52,7 +72,7 @@ fn wow_helper(h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> HelperResul
|
||||||
|
|
||||||
fn rocket() -> rocket::Rocket {
|
fn rocket() -> rocket::Rocket {
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.mount("/", routes![index, get])
|
.mount("/", routes![index, hello, about])
|
||||||
.catch(catchers![not_found])
|
.catch(catchers![not_found])
|
||||||
.attach(Template::custom(|engines| {
|
.attach(Template::custom(|engines| {
|
||||||
engines.handlebars.register_helper("wow", Box::new(wow_helper));
|
engines.handlebars.register_helper("wow", Box::new(wow_helper));
|
||||||
|
|
|
@ -40,15 +40,16 @@ 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", |_client: &Client, mut response: LocalResponse| {
|
||||||
let context = super::TemplateContext {
|
let _context = super::TemplateContext {
|
||||||
name: "Jack".into(),
|
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.status(), Status::Ok);
|
||||||
assert_eq!(response.body_string(), Some(expected));
|
assert!(response.body_string().unwrap().contains("Jack"));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
{{#*inline "page"}}
|
||||||
|
|
||||||
|
<section id="about">
|
||||||
|
<h1>Here's anoter page!</h1>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
{{/inline}}
|
||||||
|
{{~> (parent)~}}
|
|
@ -0,0 +1,3 @@
|
||||||
|
<footer>
|
||||||
|
<p>This is a footer partial.</p>
|
||||||
|
</footer>
|
|
@ -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)~}}
|
|
@ -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>
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{{title}}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{{> nav}}
|
||||||
|
{{~> page}}
|
||||||
|
{{> footer}}
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1 @@
|
||||||
|
<a href="/about">About</a>
|
Loading…
Reference in New Issue