aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-24 20:22:23 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-24 20:22:23 +0200
commit1c9066f9b40e0f4eb8fd67f6492efc96308eccd4 (patch)
tree94ace672710ca37cebd67ac45ca1c0276af6cb84
parente530ec879a53015343489c16ab7d2f93eee70b5f (diff)
downloadaskama-1c9066f9b40e0f4eb8fd67f6492efc96308eccd4.tar.gz
askama-1c9066f9b40e0f4eb8fd67f6492efc96308eccd4.tar.bz2
askama-1c9066f9b40e0f4eb8fd67f6492efc96308eccd4.zip
Add support for user-defined filters
-rw-r--r--askama/src/filters/mod.rs2
-rw-r--r--askama_derive/src/generator.rs16
2 files changed, 17 insertions, 1 deletions
diff --git a/askama/src/filters/mod.rs b/askama/src/filters/mod.rs
index ca229ca..73b311d 100644
--- a/askama/src/filters/mod.rs
+++ b/askama/src/filters/mod.rs
@@ -2,6 +2,8 @@
//!
//! Contains all the built-in filter functions for use in templates.
//! Currently, there is no way to define filters outside this module.
+//
+// WHEN ADDING FILTERS, DON'T FORGET TO UPDATE `BUILT_IN_FILTERS` in askama_derive::generator.
#[cfg(feature = "serde-json")]
mod json;
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<Node<'a>>, WS)>;
+
+const BUILT_IN_FILTERS: [&str; 9] = [
+ "e",
+ "escape",
+ "format",
+ "lower",
+ "lowercase",
+ "trim",
+ "upper",
+ "uppercase",
+ "json", // Optional feature; reserve the name anyway
+];