diff options
Diffstat (limited to '')
-rw-r--r-- | askama_actix/Cargo.toml | 22 | ||||
-rw-r--r-- | askama_actix/src/lib.rs | 22 | ||||
-rw-r--r-- | askama_actix/templates/hello.html | 1 | ||||
-rw-r--r-- | askama_actix/tests/basic.rs (renamed from testing/tests/actix_web.rs) | 3 |
4 files changed, 46 insertions, 2 deletions
diff --git a/askama_actix/Cargo.toml b/askama_actix/Cargo.toml new file mode 100644 index 0000000..1e857a5 --- /dev/null +++ b/askama_actix/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "askama_actix" +version = "0.9.0" +authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"] +description = "Actix-Web 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] +actix-web = { version = "2" } +askama = { version = "0.9.0", path = "../askama", features = ["with-actix-web", "mime", "mime_guess"] } +bytes = { version = "0.5" } +futures = { version = "0.3" } + +[dev-dependencies] +actix-rt = { version = "1", default-features = false } diff --git a/askama_actix/src/lib.rs b/askama_actix/src/lib.rs new file mode 100644 index 0000000..7372034 --- /dev/null +++ b/askama_actix/src/lib.rs @@ -0,0 +1,22 @@ +pub use askama::*; +use bytes::BytesMut; + +use actix_web::{error::ErrorInternalServerError, Error, HttpResponse}; + +pub trait TemplateIntoResponse { + fn into_response(&self) -> ::std::result::Result<HttpResponse, Error>; +} + +impl<T: askama::Template> TemplateIntoResponse for T { + fn into_response(&self) -> ::std::result::Result<HttpResponse, Error> { + let mut buffer = BytesMut::with_capacity(self.size_hint()); + self.render_into(&mut buffer) + .map_err(|_| ErrorInternalServerError("Template parsing error"))?; + + let ctype = + askama::mime::extension_to_mime_type(self.extension().unwrap_or("txt")).to_string(); + Ok(HttpResponse::Ok() + .content_type(ctype.as_str()) + .body(buffer.freeze())) + } +} diff --git a/askama_actix/templates/hello.html b/askama_actix/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_actix/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/testing/tests/actix_web.rs b/askama_actix/tests/basic.rs index 8beef3d..f4beff7 100644 --- a/testing/tests/actix_web.rs +++ b/askama_actix/tests/basic.rs @@ -1,8 +1,7 @@ -#![cfg(feature = "actix")] use actix_web::http::header::CONTENT_TYPE; use actix_web::test; use actix_web::web; -use askama::{actix_web::TemplateIntoResponse, Template}; +use askama_actix::{Template, TemplateIntoResponse}; use bytes::Bytes; #[derive(Template)] |