aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-23 20:42:46 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-23 20:42:46 +0200
commite9046c46b7bac87b3f4d89894dd21365360bd3ce (patch)
tree51943544c1a960394c593c5d4c50eee6c5a8e2a5
parent57a10a789e62be9f55ea1d28610074709015ee0f (diff)
downloadaskama-e9046c46b7bac87b3f4d89894dd21365360bd3ce.tar.gz
askama-e9046c46b7bac87b3f4d89894dd21365360bd3ce.tar.bz2
askama-e9046c46b7bac87b3f4d89894dd21365360bd3ce.zip
Optionally implement Iron's Modifier<Response> for templates
-rw-r--r--askama/Cargo.toml2
-rw-r--r--askama/src/lib.rs7
-rw-r--r--askama_derive/Cargo.toml4
-rw-r--r--askama_derive/src/generator.rs12
4 files changed, 25 insertions, 0 deletions
diff --git a/askama/Cargo.toml b/askama/Cargo.toml
index 32a8c03..f96b939 100644
--- a/askama/Cargo.toml
+++ b/askama/Cargo.toml
@@ -20,10 +20,12 @@ askama_derive = { path = "../askama_derive", version = "0.3.4" }
error-chain = "0.10"
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
+iron = { version = "0.5", optional = true }
[features]
serde-json = ["serde", "serde_json"]
default = []
+with-iron = ["iron", "askama_derive/iron"]
[package.metadata.docs.rs]
features = [ "serde-json" ]
diff --git a/askama/src/lib.rs b/askama/src/lib.rs
index 1870363..4d9b2fb 100644
--- a/askama/src/lib.rs
+++ b/askama/src/lib.rs
@@ -243,6 +243,13 @@ pub mod filters;
pub use askama_derive::*;
pub use errors::Result;
+#[cfg(feature = "with-iron")]
+pub mod iron {
+ extern crate iron;
+ pub use self::iron::modifier::Modifier;
+ pub use self::iron::response::Response;
+}
+
// Duplicates askama_derive::path::template_dir()
fn template_dir() -> PathBuf {
let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml
index 3bec13a..40f1ad5 100644
--- a/askama_derive/Cargo.toml
+++ b/askama_derive/Cargo.toml
@@ -11,6 +11,10 @@ workspace = ".."
[lib]
proc-macro = true
+[features]
+default = []
+iron = []
+
[dependencies]
nom = "3"
syn = "0.11"
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 6eb16e8..57d54b1 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -53,6 +53,9 @@ pub fn generate(ast: &syn::DeriveInput, path: &Path, mut nodes: Vec<Node>) -> St
gen.impl_template(ast, &content);
}
gen.impl_display(ast);
+ if cfg!(feature = "iron") {
+ gen.impl_modifier_response(ast);
+ }
gen.result()
}
@@ -618,6 +621,15 @@ impl<'a> Generator<'a> {
self.writeln("}");
}
+ // Implement iron's Modifier<Response> if enabled
+ fn impl_modifier_response(&mut self, ast: &syn::DeriveInput) {
+ self.write_header(ast, "::askama::iron::Modifier<::askama::iron::Response>");
+ self.writeln("fn modify(self, res: &mut ::askama::iron::Response) {");
+ self.writeln("res.body = Some(Box::new(self.render().unwrap().into_bytes()));");
+ self.writeln("}");
+ self.writeln("}");
+ }
+
fn result(self) -> String {
self.buf
}