mirror of https://github.com/rwf2/Rocket.git
Added Tera templates example.
This commit is contained in:
parent
d136332e91
commit
7bda1b527b
|
@ -25,6 +25,7 @@ members = [
|
|||
"examples/json",
|
||||
"examples/msgpack",
|
||||
"examples/handlebars_templates",
|
||||
"examples/tera_templates",
|
||||
"examples/form_kitchen_sink",
|
||||
"examples/config",
|
||||
"examples/raw_upload",
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
[package]
|
||||
name = "tera_templates"
|
||||
version = "0.0.0"
|
||||
workspace = "../../"
|
||||
|
||||
[dependencies]
|
||||
rocket = { path = "../../lib" }
|
||||
rocket_codegen = { path = "../../codegen" }
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
||||
[dependencies.rocket_contrib]
|
||||
path = "../../contrib"
|
||||
default-features = false
|
||||
features = ["tera_templates"]
|
|
@ -0,0 +1,52 @@
|
|||
#![feature(plugin)]
|
||||
#![plugin(rocket_codegen)]
|
||||
|
||||
extern crate rocket_contrib;
|
||||
extern crate rocket;
|
||||
extern crate serde_json;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
|
||||
#[cfg(test)] mod tests;
|
||||
|
||||
use rocket::Request;
|
||||
use rocket::response::Redirect;
|
||||
use rocket_contrib::Template;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TemplateContext {
|
||||
name: String,
|
||||
items: Vec<String>
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> Redirect {
|
||||
Redirect::to("/hello/Unknown")
|
||||
}
|
||||
|
||||
#[get("/hello/<name>")]
|
||||
fn get(name: String) -> Template {
|
||||
let context = TemplateContext {
|
||||
name: name,
|
||||
items: vec!["One".to_owned(), "Two".to_owned(), "Three".to_owned()]
|
||||
};
|
||||
|
||||
Template::render("index", &context)
|
||||
}
|
||||
|
||||
#[error(404)]
|
||||
fn not_found(req: &Request) -> Template {
|
||||
let mut map = std::collections::HashMap::new();
|
||||
map.insert("path", req.uri().as_str());
|
||||
Template::render("error/404", &map)
|
||||
}
|
||||
|
||||
fn rocket() -> rocket::Rocket {
|
||||
rocket::ignite()
|
||||
.mount("/", routes![index, get])
|
||||
.attach(Template::fairing())
|
||||
.catch(errors![not_found])
|
||||
}
|
||||
|
||||
fn main() {
|
||||
rocket().launch();
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
use rocket;
|
||||
use super::rocket;
|
||||
use rocket::local::{Client, LocalResponse};
|
||||
use rocket::http::Method::*;
|
||||
use rocket::http::Status;
|
||||
use rocket_contrib::Template;
|
||||
|
||||
const TEMPLATE_ROOT: &'static str = "templates/";
|
||||
|
||||
macro_rules! dispatch {
|
||||
($method:expr, $path:expr, $test_fn:expr) => ({
|
||||
let client = Client::new(rocket()).unwrap();
|
||||
$test_fn(client.req($method, $path).dispatch());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_root() {
|
||||
// Check that the redirect works.
|
||||
for method in &[Get, Head] {
|
||||
dispatch!(*method, "/", |mut response: LocalResponse| {
|
||||
assert_eq!(response.status(), Status::SeeOther);
|
||||
assert!(response.body().is_none());
|
||||
|
||||
let location: Vec<_> = response.headers().get("Location").collect();
|
||||
assert_eq!(location, vec!["/hello/Unknown"]);
|
||||
|
||||
let req = MockRequest::new(*method, "/");
|
||||
run_test!(req, |mut response: Response| {
|
||||
assert_eq!(response.status(), Status::SeeOther);
|
||||
assert!(response.body().is_none());
|
||||
|
||||
let location_headers: Vec<_> = response.headers().get("Location").collect();
|
||||
assert_eq!(location_headers, vec!["/hello/Unknown"]);
|
||||
|
||||
dispatch!(*method, "/", |mut response: Response| {
|
||||
assert_eq!(response.status(), Status::SeeOther);
|
||||
assert!(response.body().is_none());
|
||||
|
||||
let location: Vec<_> = response.headers().get("Location").collect();
|
||||
assert_eq!(location, vec!["/hello/Unknown"]);
|
||||
});
|
||||
}
|
||||
|
||||
// Check that other request methods are not accepted (and instead caught).
|
||||
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
|
||||
dispatch!(*method, "/", |mut response: LocalResponse| {
|
||||
let mut map = ::std::collections::HashMap::new();
|
||||
map.insert("path", "/");
|
||||
let expected = Template::show(TEMPLATE_ROOT, "error/404", &map).unwrap();
|
||||
|
||||
assert_eq!(response.status(), Status::NotFound);
|
||||
assert_eq!(response.body_string(), Some(expected));
|
||||
|
||||
let req = MockRequest::new(*method, "/");
|
||||
run_test!(req, |mut response: Response| {
|
||||
let mut map = ::std::collections::HashMap::new();
|
||||
map.insert("path", "/");
|
||||
let expected = Template::show(TEMPLATE_ROOT, "error/404", &map).unwrap();
|
||||
|
||||
assert_eq!(response.status(), Status::NotFound);
|
||||
assert_eq!(response.body_string(), Some(expected_body));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_name() {
|
||||
// Check that the /hello/<name> route works.
|
||||
dispatch!(Get, "/hello/Jack", |mut response: LocalResponse| {
|
||||
let context = super::TemplateContext {
|
||||
name: "Jack".to_string(),
|
||||
items: vec!["One", "Two", "Three"].iter().map(|s| s.to_string()).collect()
|
||||
};
|
||||
|
||||
let expected = Template::show(TEMPLATE_ROOT, "index", &context).unwrap();
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
let expected = Template::render("index", &context).to_string();
|
||||
assert_eq!(response.body_string(), Some(expected));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_404() {
|
||||
// Check that the error catcher works.
|
||||
dispatch!(Get, "/hello/", |mut response: LocalResponse| {
|
||||
let mut map = ::std::collections::HashMap::new();
|
||||
map.insert("path", "/hello/");
|
||||
|
||||
let expected = Template::show(TEMPLATE_ROOT, "error/404", &map).unwrap();
|
||||
assert_eq!(response.status(), Status::NotFound);
|
||||
|
||||
dispatch!(Get, "/hello/", |mut response: Response| {
|
||||
let mut map = ::std::collections::HashMap::new();
|
||||
map.insert("path", "/hello/");
|
||||
|
||||
let expected = Template::show(TEMPLATE_ROOT, "error/404", &map).unwrap();
|
||||
assert_eq!(response.status(), Status::NotFound);
|
||||
});
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Tera Demo</title>
|
||||
</head>
|
||||
<body>
|
||||
{% block content %}{% endblock content %}
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>404</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>404: Hey! There's nothing here.</h1>
|
||||
The page at {{ path }} does not exist!
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,15 @@
|
|||
{% extends "base" %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h1>Hi {{name}}</h1>
|
||||
<h3>Here are your items:</h3>
|
||||
<ul>
|
||||
{% for s in items %}
|
||||
<li>{{ s }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<p>Try going to <a href="/hello/YourName">/hello/YourName</a></p>
|
||||
|
||||
{% endblock content %}
|
Loading…
Reference in New Issue