From 1c9066f9b40e0f4eb8fd67f6492efc96308eccd4 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 24 Aug 2017 20:22:23 +0200 Subject: Add support for user-defined filters --- askama_derive/src/generator.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'askama_derive/src') diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 1d8e8f2..c0986da 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -228,8 +228,10 @@ impl<'a> Generator<'a> { fn visit_filter(&mut self, name: &str, args: &[Expr]) { if name == "format" { self.write("format!("); - } else { + } else if BUILT_IN_FILTERS.contains(&name) { self.write(&format!("::askama::filters::{}(&", name)); + } else { + self.write(&format!("filters::{}(&", name)); } for (i, arg) in args.iter().enumerate() { @@ -707,3 +709,15 @@ impl<'a, T: 'a> SetChain<'a, T> where T: cmp::Eq + hash::Hash { } type MacroMap<'a> = HashMap<&'a str, (WS, &'a str, Vec<&'a str>, Vec>, WS)>; + +const BUILT_IN_FILTERS: [&str; 9] = [ + "e", + "escape", + "format", + "lower", + "lowercase", + "trim", + "upper", + "uppercase", + "json", // Optional feature; reserve the name anyway +]; -- cgit