From 9296d1303ed7baa88507e683c23319c75df341d8 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 18 Feb 2017 13:15:49 +0100 Subject: Add support for attributes --- askama/src/generator.rs | 6 ++++++ askama/src/parser.rs | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) 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>, &'a str), Filter(&'a str, Box>), BinOp(&'a str, Box>, Box>), } @@ -96,6 +97,15 @@ named!(expr_single, alt!( expr_var )); +named!(expr_attr, 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, do_parse!( - obj: expr_single >> + obj: expr_attr >> filters: many0!(filter) >> ({ let mut res = obj; -- cgit