diff options
-rw-r--r-- | askama/src/generator.rs | 6 | ||||
-rw-r--r-- | askama/src/parser.rs | 12 |
2 files changed, 17 insertions, 1 deletions
diff --git a/askama/src/generator.rs b/askama/src/generator.rs index a8d62af..4ebc61a 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -119,6 +119,11 @@ impl<'a> Generator<'a> { } } + fn visit_attr(&mut self, obj: &Expr, attr: &str) { + self.visit_expr(obj); + self.write(&format!(".{}", attr)); + } + fn visit_filter(&mut self, name: &str, val: &Expr) { self.write(&format!("askama::filters::{}(&", name)); self.visit_expr(val); @@ -136,6 +141,7 @@ impl<'a> Generator<'a> { Expr::NumLit(s) => self.visit_num_lit(s), Expr::StrLit(s) => self.visit_str_lit(s), Expr::Var(s) => self.visit_var(s), + Expr::Attr(ref obj, name) => self.visit_attr(obj, name), Expr::Filter(name, ref val) => self.visit_filter(name, val), Expr::BinOp(op, ref left, ref right) => self.visit_binop(op, left, right), diff --git a/askama/src/parser.rs b/askama/src/parser.rs index 1d34611..b5583c3 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -6,6 +6,7 @@ pub enum Expr<'a> { NumLit(&'a str), StrLit(&'a str), Var(&'a str), + Attr(Box<Expr<'a>>, &'a str), Filter(&'a str, Box<Expr<'a>>), BinOp(&'a str, Box<Expr<'a>>, Box<Expr<'a>>), } @@ -96,6 +97,15 @@ named!(expr_single<Expr>, alt!( expr_var )); +named!(expr_attr<Expr>, alt!( + do_parse!( + obj: expr_single >> + tag_s!(".") >> + attr: alphanumeric >> + (Expr::Attr(Box::new(obj), str::from_utf8(attr).unwrap())) + ) | expr_single +)); + named!(filter, do_parse!( tag_s!("|") >> fname: alphanumeric >> @@ -103,7 +113,7 @@ named!(filter, do_parse!( )); named!(expr_filtered<Expr>, do_parse!( - obj: expr_single >> + obj: expr_attr >> filters: many0!(filter) >> ({ let mut res = obj; |