diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-01-28 22:06:11 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-01-29 09:25:59 +0100 |
commit | c6f9a053c7328e6c782508114bd96aa569b5de7d (patch) | |
tree | 362ef1f8ef421eb5913fa2834c10f6743e1ca7e1 /askama_rocket | |
parent | b56c11639f9ea5ef1354a1e91ca98541a16bca9b (diff) | |
download | askama-c6f9a053c7328e6c782508114bd96aa569b5de7d.tar.gz askama-c6f9a053c7328e6c782508114bd96aa569b5de7d.tar.bz2 askama-c6f9a053c7328e6c782508114bd96aa569b5de7d.zip |
Move Rocket integration into askama_rocket crate
Diffstat (limited to 'askama_rocket')
-rw-r--r-- | askama_rocket/Cargo.toml | 17 | ||||
-rw-r--r-- | askama_rocket/src/lib.rs | 16 | ||||
-rw-r--r-- | askama_rocket/templates/hello.html | 1 | ||||
-rw-r--r-- | askama_rocket/tests/basic.rs | 30 |
4 files changed, 64 insertions, 0 deletions
diff --git a/askama_rocket/Cargo.toml b/askama_rocket/Cargo.toml new file mode 100644 index 0000000..e557a38 --- /dev/null +++ b/askama_rocket/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "askama_rocket" +version = "0.9.0" +authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"] +description = "Rocket integration for Askama templates" +documentation = "https://docs.rs/askama" +keywords = ["markup", "template", "jinja2", "html"] +categories = ["template-engine"] +homepage = "https://github.com/djc/askama" +repository = "https://github.com/djc/askama" +license = "MIT OR Apache-2.0" +workspace = ".." +edition = "2018" + +[dependencies] +askama = { version = "0.9.0", path = "../askama", features = ["with-rocket", "mime", "mime_guess"] } +rocket = { version = "0.4", default-features = false } diff --git a/askama_rocket/src/lib.rs b/askama_rocket/src/lib.rs new file mode 100644 index 0000000..6db3b7b --- /dev/null +++ b/askama_rocket/src/lib.rs @@ -0,0 +1,16 @@ +use std::io::Cursor; + +pub use askama::*; +use rocket::http::{ContentType, Status}; +pub use rocket::request::Request; +use rocket::response::Response; +pub use rocket::response::{Responder, Result}; + +pub fn respond<T: Template>(t: &T, ext: &str) -> Result<'static> { + let rsp = t.render().map_err(|_| Status::InternalServerError)?; + let ctype = ContentType::from_extension(ext).ok_or(Status::InternalServerError)?; + Response::build() + .header(ctype) + .sized_body(Cursor::new(rsp)) + .ok() +} diff --git a/askama_rocket/templates/hello.html b/askama_rocket/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_rocket/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_rocket/tests/basic.rs b/askama_rocket/tests/basic.rs new file mode 100644 index 0000000..0671c4c --- /dev/null +++ b/askama_rocket/tests/basic.rs @@ -0,0 +1,30 @@ +#![feature(proc_macro_hygiene, decl_macro)] + +#[macro_use] +extern crate rocket; + +use askama::Template; + +use rocket::http::{ContentType, Status}; +use rocket::local::Client; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} + +#[get("/")] +fn hello() -> HelloTemplate<'static> { + HelloTemplate { name: "world" } +} + +#[test] +fn test_rocket() { + let rocket = rocket::ignite().mount("/", routes![hello]); + let client = Client::new(rocket).unwrap(); + let mut rsp = client.get("/").dispatch(); + assert_eq!(rsp.status(), Status::Ok); + assert_eq!(rsp.content_type(), Some(ContentType::HTML)); + assert_eq!(rsp.body_string().unwrap(), "Hello, world!"); +} |