diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-08-22 20:22:33 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-08-22 20:22:33 +0200 |
commit | 26a000cd9e6ab48325fbd4990bf2a3d9e2100a67 (patch) | |
tree | ffe24633dc1b60642e12ead28b2ad5dcb4fb3432 /askama_derive/src/generator.rs | |
parent | 544f9b11926b6ccad1bc4e7a6842c5421f9f1576 (diff) | |
download | askama-26a000cd9e6ab48325fbd4990bf2a3d9e2100a67.tar.gz askama-26a000cd9e6ab48325fbd4990bf2a3d9e2100a67.tar.bz2 askama-26a000cd9e6ab48325fbd4990bf2a3d9e2100a67.zip |
Add support for calling macros
Diffstat (limited to 'askama_derive/src/generator.rs')
-rw-r--r-- | askama_derive/src/generator.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index dd34bec..6eb16e8 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -317,6 +317,25 @@ impl<'a> Generator<'a> { self.writeln("))?;"); } + fn write_call(&mut self, ws: &WS, name: &str, args: &[Expr]) { + self.handle_ws(ws); + let def = self.macros.get(name).expect(&format!("macro '{}' not found", name)); + self.locals.push(); + self.writeln("{"); + self.prepare_ws(&def.0); + for (i, arg) in def.2.iter().enumerate() { + self.write(&format!("let {} = &", arg)); + self.locals.insert(arg); + self.visit_expr(&args.get(i) + .expect(&format!("macro '{}' takes more than {} arguments", name, i))); + self.writeln(";"); + } + self.handle(&def.3); + self.flush_ws(&def.4); + self.writeln("}"); + self.locals.pop(); + } + fn write_let_decl(&mut self, ws: &WS, var: &'a Target) { self.handle_ws(ws); self.write("let "); @@ -453,6 +472,7 @@ impl<'a> Generator<'a> { Node::Include(ref ws, ref path) => { self.handle_include(ws, path); }, + Node::Call(ref ws, name, ref args) => self.write_call(ws, name, args), Node::Macro(_, _, _, _, _) | Node::Extends(_) => { panic!("no extends or macros allowed in content"); |