From e9046c46b7bac87b3f4d89894dd21365360bd3ce Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 23 Aug 2017 20:42:46 +0200 Subject: Optionally implement Iron's Modifier for templates --- askama_derive/src/generator.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'askama_derive/src') 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) -> 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 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 } -- cgit