Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add override kwarg to register.filter. Fix #20.

  • Loading branch information...
commit f7059317114580ba2cce07d7c914cc2217633567 1 parent 1dc0e0b
James Socol authored
Showing with 29 additions and 3 deletions.
  1. +11 −3 jingo/__init__.py
  2. +18 −0 jingo/tests/test_helpers.py
View
14 jingo/__init__.py
@@ -124,10 +124,18 @@ class Register(object):
def __init__(self, env):
self.env = env
- # TODO: this should be overridable too.
- def filter(self, f):
+ def filter(self, f=None, override=True):
"""Adds the decorated function to Jinja's filter library."""
- self.env.filters[f.__name__] = f
+ def decorator(f):
+ @functools.wraps(f)
+ def wrapper(*args, **kw):
+ return f(*args, **kw)
+ return self.filter(wrapper, override)
+
+ if not f:
+ return decorator
+ if override or f.__name__ not in self.env.filters:
+ self.env.filters[f.__name__] = f
return f
def function(self, f=None, override=True):
View
18 jingo/tests/test_helpers.py
@@ -150,3 +150,21 @@ def test_custom_url(s):
# teardown
register.function(helpers.url, override=True)
+
+
+def test_filter_override():
+ def f(s): return s.upper()
+ f.__name__ = 'a'
+ register.filter(f)
+ s = render('{{ s|a }}', {'s': 'Str'})
+ eq_(s, 'STR')
+
+ def g(s): return s.lower()
+ g.__name__ = 'a'
+ register.filter(override=False)(g)
+ s = render('{{ s|a }}', {'s': 'Str'})
+ eq_(s, 'STR')
+
+ register.filter(g)
+ s = render('{{ s|a }}', {'s': 'Str'})
+ eq_(s, 'str')
Please sign in to comment.
Something went wrong with that request. Please try again.