Browse Source

the template_filter now expects the parentheses

pull/1638/head
Sebastien Estienne 15 years ago committed by Armin Ronacher
parent
commit
5c9ef2c44d
  1. 10
      flask.py
  2. 11
      tests/flask_tests.py

10
flask.py

@ -640,24 +640,20 @@ class Flask(object):
return f
return decorator
def template_filter(self, arg=None):
def template_filter(self, name=None):
"""A decorator that is used to register custom template filter.
You can specify a name for the filter, otherwise the function
name will be used. Example::
@app.template_filter
@app.template_filter()
def reverse(s):
return s[::-1]
:param name: the optional name of the filter, otherwise the
function name will be used.
"""
if type(arg) is types.FunctionType:
self.jinja_env.filters[arg.__name__] = arg
return arg
def decorator(f):
self.jinja_env.filters[arg or f.__name__] = f
self.jinja_env.filters[name or f.__name__] = f
return f
return decorator

11
tests/flask_tests.py

@ -311,16 +311,7 @@ class TemplatingTestCase(unittest.TestCase):
macro = flask.get_template_attribute('_macro.html', 'hello')
assert macro('World') == 'Hello World!'
def test_template_filter_not_called(self):
app = flask.Flask(__name__)
@app.template_filter
def my_reverse(s):
return s[::-1]
assert 'my_reverse' in app.jinja_env.filters.keys()
assert app.jinja_env.filters['my_reverse'] == my_reverse
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
def test_template_filter_called(self):
def test_template_filter(self):
app = flask.Flask(__name__)
@app.template_filter()
def my_reverse(s):

Loading…
Cancel
Save