aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive
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 /askama_derive
parent57a10a789e62be9f55ea1d28610074709015ee0f (diff)
downloadaskama-e9046c46b7bac87b3f4d89894dd21365360bd3ce.tar.gz
askama-e9046c46b7bac87b3f4d89894dd21365360bd3ce.tar.bz2
askama-e9046c46b7bac87b3f4d89894dd21365360bd3ce.zip
Optionally implement Iron's Modifier<Response> for templates
Diffstat (limited to '')
-rw-r--r--askama_derive/Cargo.toml4
-rw-r--r--askama_derive/src/generator.rs12
2 files changed, 16 insertions, 0 deletions
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
}