Anonymous avatar Anonymous committed 41a2c74

add a decorator to add custom template filter

Comments (0)

Files changed (2)

 from __future__ import with_statement
 import os
 import sys
+import types
 
 from jinja2 import Environment, PackageLoader, FileSystemLoader
 from werkzeug import Request as RequestBase, Response as ResponseBase, \
             return f
         return decorator
 
+    def template_filter(self, arg=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
+          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
+            return f
+        return decorator
+
     def before_request(self, f):
         """Registers a function to run before each request."""
         self.before_request_funcs.append(f)

tests/flask_tests.py

             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):
+        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_with_name(self):
+        app = flask.Flask(__name__)
+        @app.template_filter('strrev')
+        def my_reverse(s):
+            return s[::-1]
+        assert 'strrev' in  app.jinja_env.filters.keys()
+        assert app.jinja_env.filters['strrev'] == my_reverse
+        assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
 
 def suite():
     from minitwit_tests import MiniTwitTestCase
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.