aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src/generator.rs
diff options
context:
space:
mode:
authorLibravatar larros <larserik.rosengren@gmail.com>2017-09-26 10:02:44 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-09-26 10:02:44 +0200
commitdf58dcb49db77b2c283db3c9a8a738bdff3a8e37 (patch)
tree0de4a117d1e97216381b8336a1cec826e894d075 /askama_shared/src/generator.rs
parent4e919e47539b5fa346f1cac323ab250558b834bc (diff)
downloadaskama-df58dcb49db77b2c283db3c9a8a738bdff3a8e37.tar.gz
askama-df58dcb49db77b2c283db3c9a8a738bdff3a8e37.tar.bz2
askama-df58dcb49db77b2c283db3c9a8a738bdff3a8e37.zip
Add support for importing template files with macros (#51)
* Fix review comments for macro imports
Diffstat (limited to '')
-rw-r--r--askama_shared/src/generator.rs10
1 files changed, 7 insertions, 3 deletions
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");