forked from orbit-oss/flask
Add @template_test() decorator for creating custom jinja2 tests, like existing @template_filter() for filters. Fixes #332
This commit is contained in:
parent
b2fc9febdd
commit
f034d8d345
5 changed files with 265 additions and 11 deletions
38
flask/app.py
38
flask/app.py
|
|
@ -1086,6 +1086,44 @@ class Flask(_PackageBoundObject):
|
|||
"""
|
||||
self.jinja_env.filters[name or f.__name__] = f
|
||||
|
||||
@setupmethod
|
||||
def template_test(self, name=None):
|
||||
"""A decorator that is used to register custom template test.
|
||||
You can specify a name for the test, otherwise the function
|
||||
name will be used. Example::
|
||||
|
||||
@app.template_test()
|
||||
def is_prime(n):
|
||||
if n == 2:
|
||||
return True
|
||||
for i in xrange(2, int(math.ceil(math.sqrt(n))) + 1):
|
||||
if n % i == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
.. versionadded:: 0.10
|
||||
|
||||
:param name: the optional name of the test, otherwise the
|
||||
function name will be used.
|
||||
"""
|
||||
def decorator(f):
|
||||
self.add_template_test(f, name=name)
|
||||
return f
|
||||
return decorator
|
||||
|
||||
@setupmethod
|
||||
def add_template_test(self, f, name=None):
|
||||
"""Register a custom template test. Works exactly like the
|
||||
:meth:`template_test` decorator.
|
||||
|
||||
.. versionadded:: 0.10
|
||||
|
||||
:param name: the optional name of the test, otherwise the
|
||||
function name will be used.
|
||||
"""
|
||||
self.jinja_env.tests[name or f.__name__] = f
|
||||
|
||||
|
||||
@setupmethod
|
||||
def before_request(self, f):
|
||||
"""Registers a function to run before each request."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue