diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-18 13:15:49 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-18 13:15:49 +0100 |
commit | 9296d1303ed7baa88507e683c23319c75df341d8 (patch) | |
tree | dac937bf52bb62affeaf408ed6181f65aa2b371c | |
parent | 7a6e79187ee1a86d0c71f160ac00fa9f5a290134 (diff) | |
download | askama-9296d1303ed7baa88507e683c23319c75df341d8.tar.gz askama-9296d1303ed7baa88507e683c23319c75df341d8.tar.bz2 askama-9296d1303ed7baa88507e683c23319c75df341d8.zip |
Add support for attributes
Diffstat (limited to '')
-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; |