From df58dcb49db77b2c283db3c9a8a738bdff3a8e37 Mon Sep 17 00:00:00 2001 From: larros Date: Tue, 26 Sep 2017 10:02:44 +0200 Subject: Add support for importing template files with macros (#51) * Fix review comments for macro imports --- askama_shared/src/generator.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'askama_shared/src/generator.rs') diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index e92a2c6..e135c13 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -12,8 +12,8 @@ use std::collections::{HashMap, HashSet}; use syn; -pub fn generate(input: &TemplateInput, nodes: &[Node]) -> String { - Generator::default().build(&State::new(input, nodes)) +pub fn generate(input: &TemplateInput, nodes: &[Node], imported: &HashMap<&str, Macro>) -> String { + Generator::default().build(&State::new(input, nodes, imported)) } struct State<'a> { @@ -26,7 +26,7 @@ struct State<'a> { } impl<'a> State<'a> { - fn new<'n>(input: &'n TemplateInput, nodes: &'n [Node]) -> State<'n> { + fn new<'n>(input: &'n TemplateInput, nodes: &'n [Node], imported: &'n HashMap<&'n str, Macro<'n>>) -> State<'n> { let mut base: Option<&Expr> = None; let mut blocks = Vec::new(); let mut macros = HashMap::new(); @@ -47,6 +47,9 @@ impl<'a> State<'a> { _ => {}, } } + for (name, ref m) in imported { + macros.insert(name, m); + } State { input, nodes, @@ -351,6 +354,7 @@ impl<'a> Generator<'a> { }, Node::Call(ref ws, name, ref args) => self.write_call(state, ws, name, args), Node::Macro(_, _) | + Node::Import(_, _) | Node::Extends(_) => { if let AstLevel::Nested = level { panic!("macro or extend blocks only allowed at the top level"); -- cgit