From ce4d589d5b7bbda46b9c34211dcd3e1c90ee83c9 Mon Sep 17 00:00:00 2001 From: Ron DuPlain Date: Mon, 16 Jan 2012 20:16:48 -0500 Subject: [PATCH] Add non-decorator template filter methods. Suggested by @Poincare on GitHub, on @Reisen's pull request: https://github.com/mitsuhiko/flask/pull/272 --- flask/app.py | 12 +++++++++++- flask/blueprints.py | 25 +++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/flask/app.py b/flask/app.py index 42ffea4d..0e462020 100644 --- a/flask/app.py +++ b/flask/app.py @@ -1018,10 +1018,20 @@ class Flask(_PackageBoundObject): function name will be used. """ def decorator(f): - self.jinja_env.filters[name or f.__name__] = f + self.add_template_filter(f, name=name) return f return decorator + @setupmethod + def add_template_filter(self, f, name=None): + """Register a custom template filter. Works exactly like the + :meth:`template_filter` decorator. + + :param name: the optional name of the filter, otherwise the + function name will be used. + """ + self.jinja_env.filters[name or f.__name__] = f + @setupmethod def before_request(self, f): """Registers a function to run before each request.""" diff --git a/flask/blueprints.py b/flask/blueprints.py index 54fa33bc..d81d3c73 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -185,17 +185,30 @@ class Blueprint(_PackageBoundObject): return f return decorator - def app_template_filter(self, name = None): - """Like :meth:`Flask.template_filter` but for a blueprint. The filter - is available for the entire application. + def app_template_filter(self, name=None): + """Register a custom template filter, available application wide. Like + :meth:`Flask.template_filter` but for a blueprint. + + :param name: the optional name of the filter, otherwise the + function name will be used. """ def decorator(f): - def register_template(state): - state.app.jinja_env.filters[name or f.__name__] = f - self.record_once(register_template) + self.add_app_template_filter(f, name=name) return f return decorator + def add_app_template_filter(self, f, name=None): + """Register a custom template filter, available application wide. Like + :meth:`Flask.add_template_filter` but for a blueprint. Works exactly + like the :meth:`app_template_filter` decorator. + + :param name: the optional name of the filter, otherwise the + function name will be used. + """ + def register_template(state): + state.app.jinja_env.filters[name or f.__name__] = f + self.record_once(register_template) + def before_request(self, f): """Like :meth:`Flask.before_request` but for a blueprint. This function is only executed before each request that is handled by a function of