aboutsummaryrefslogtreecommitdiffstats
path: root/askama_rocket
diff options
context:
space:
mode:
Diffstat (limited to 'askama_rocket')
-rw-r--r--askama_rocket/Cargo.toml17
-rw-r--r--askama_rocket/src/lib.rs16
-rw-r--r--askama_rocket/templates/hello.html1
-rw-r--r--askama_rocket/tests/basic.rs30
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!");
+}