From ff24eef1ff7dcf3849fd1e6d9efec0de2bc57005 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 28 Jan 2020 22:49:04 +0100 Subject: Move Iron integration into a separate askama_iron crate --- askama_iron/Cargo.toml | 17 +++++++++++++++++ askama_iron/src/lib.rs | 3 +++ askama_iron/templates/hello.html | 1 + askama_iron/templates/hello.txt | 1 + askama_iron/tests/basic.rs | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 askama_iron/Cargo.toml create mode 100644 askama_iron/src/lib.rs create mode 100644 askama_iron/templates/hello.html create mode 100644 askama_iron/templates/hello.txt create mode 100644 askama_iron/tests/basic.rs (limited to 'askama_iron') diff --git a/askama_iron/Cargo.toml b/askama_iron/Cargo.toml new file mode 100644 index 0000000..35c585d --- /dev/null +++ b/askama_iron/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "askama_iron" +version = "0.9.0" +authors = ["Dirkjan Ochtman "] +description = "Iron 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-iron"] } +iron = { version = ">= 0.5, < 0.7" } diff --git a/askama_iron/src/lib.rs b/askama_iron/src/lib.rs new file mode 100644 index 0000000..37e79cc --- /dev/null +++ b/askama_iron/src/lib.rs @@ -0,0 +1,3 @@ +pub use askama::*; + +pub use iron::{headers::ContentType, modifier::Modifier, Response}; diff --git a/askama_iron/templates/hello.html b/askama_iron/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_iron/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_iron/templates/hello.txt b/askama_iron/templates/hello.txt new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/askama_iron/templates/hello.txt @@ -0,0 +1 @@ +Hello, {{ name }}! diff --git a/askama_iron/tests/basic.rs b/askama_iron/tests/basic.rs new file mode 100644 index 0000000..32ca839 --- /dev/null +++ b/askama_iron/tests/basic.rs @@ -0,0 +1,36 @@ +use askama::Template; +use iron::{status, Response}; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} + +#[derive(Template)] +#[template(path = "hello.txt")] +struct HelloTextTemplate<'a> { + name: &'a str, +} + +#[test] +fn test_iron() { + let rsp = Response::with((status::Ok, HelloTemplate { name: "world" })); + let mut buf = Vec::new(); + let _ = rsp.body.unwrap().write_body(&mut buf); + assert_eq!(buf, b"Hello, world!"); + + let content_type = rsp.headers.get::().unwrap(); + assert_eq!(format!("{}", content_type), "text/html; charset=utf-8"); +} + +#[test] +fn test_iron_non_html() { + let rsp = Response::with((status::Ok, HelloTextTemplate { name: "world" })); + let mut buf = Vec::new(); + let _ = rsp.body.unwrap().write_body(&mut buf); + assert_eq!(buf, b"Hello, world!"); + + let content_type = rsp.headers.get::(); + assert_eq!(content_type, None); +} -- cgit