9 unstable releases
Uses old Rust 2015
0.5.0 | Apr 19, 2018 |
---|---|
0.4.0 | Jun 30, 2017 |
0.3.0 | Apr 23, 2017 |
0.2.0 | Feb 3, 2017 |
0.1.1 | Dec 28, 2016 |
#417 in Template engine
39 downloads per month
17KB
203 lines
iron-tera
This is a Tera middleware for Iron.
Check me out on crates.io or read the documentation.
After the initial template engine is created, you can render templates in a given handler using either a Tera Context
, or a value that implementes serde's Serialize
.
Unstable
fn main() {
let mut router = Router::new();
router.get("/user", user_handler, "user");
router.get("/usertest", produce_handler, "usertest");
let mut chain = Chain::new(router);
let teng = TeraEngine::new("src/examples/templates/**/*");
chain.link_after(teng);
Iron::new(chain).http("localhost:5000").unwrap();
}
fn user_handler(_: &mut Request) -> IronResult<Response> {
let mut resp = Response::new();
let mut context = Context::new();
context.add("username", &"Bob");
context.add("my_var", &"Thing");
context.add("numbers", &vec![1, 2, 3]);
context.add("bio", &"<script>alert('pwnd');</script>");
resp.set_mut(Template::new(
"users/profile.html",
TemplateMode::from_context(context),
)).set_mut(status::Ok);
Ok(resp)
}
// this uses the unstable feature on nightly
fn produce_handler(_: &mut Request) -> IronResult<Response> {
let mut resp = Response::new();
let user = User {
username: "Bob",
my_var: "Thing",
numbers: &vec![1, 2, 3],
bio: "<script>alert('pwnd');</script>",
};
match serde_json::to_value(user) {
Ok(u) => match Template::new("users/profile.html", u) {
Ok(t) => {
resp.set_mut(t).set_mut(status::Ok);
Ok(resp)
}
Err(_) => Err(IronError::new(
StringError("Template Error".to_string()),
status::BadRequest,
)),
},
Err(_) => Err(IronError::new(
StringError("Serialization error".to_string()),
status::BadRequest,
)),
}
}
Stable
fn main() {
let mut router = Router::new();
router.get("/user", user_handler, "user");
router.get("/usertest", produce_handler, "usertest");
router.get("/blob", blob_handler, "blobtest");
let mut chain = Chain::new(router);
let teng = TeraEngine::new("src/examples/templates/**/*");
chain.link_after(teng);
Iron::new(chain).http("localhost:5000").unwrap();
}
fn user_handler(_: &mut Request) -> IronResult<Response> {
let mut resp = Response::new();
let mut context = Context::new();
context.add("username", &"Bob");
context.add("my_var", &"Thing"); // comment out to see alternate thing
context.add("numbers", &vec![1, 2, 3]);
context.add("bio", &"<script>alert('pwnd');</script>");
resp.set_mut(Template::new(
"users/profile.html",
TemplateMode::from_context(context),
)).set_mut(status::Ok);
Ok(resp)
}
// this uses the unstable feature on nightly
fn produce_handler(_: &mut Request) -> IronResult<Response> {
let mut resp = Response::new();
let user = User {
username: "Bob",
my_var: "Thing",
numbers: &vec![1, 2, 3],
bio: "<script>alert('pwnd');</script>",
};
match serde_json::to_value(user) {
Ok(u) => {
resp.set_mut(Template::new("users/profile.html", u))
.set_mut(status::Ok);
Ok(resp)
}
Err(_) => Err(IronError::new(
StringError("Serialization error".to_string()),
status::BadRequest,
)),
}
}
Dependencies
~13–21MB
~334K SLoC