Steve Losh avatar Steve Losh committed 4a22130

Add the ability to define custom Jinja filters.

Comments (0)

Files changed (2)

blatter/__init__.py

                 for fallback in flattened_fallback_configs_for(config))
 
     loader = jinja2.loaders.ChoiceLoader(path)
-    return jinja2.Environment(loader=loader)
+    env = jinja2.Environment(loader=loader)
+    for extra_filter in filter_configs_for(config):
+        f = extra_filter['function']
+        f_module_path = '.'.join(f.split('.')[:-1])
+        f_filter_name = f.split('.')[-1]
+        mod = __import__(f_filter_name, fromlist=[f_module_path])
+        env.filters[extra_filter['name']] = getattr(mod, f.split('.')[-1])
+    return env
 
 def template_viewer_factory(config):
     """Construct an app that renders and returns templates."""
             continue
     raise StopIteration()
 
+def filter_configs_for(config):
+    """Yield configurations for Jinja filters listed in config.filters."""
+    for spec in config.get('filters', '').split(','):
+        name = spec.strip()
+        if not name:
+            continue
+        bucket = 'filter.%s' % name
+        if bucket not in config:
+            print "No [%s] configuration set, skipping." % bucket
+            continue
+        filter_name = config[bucket].get('name', '').strip()
+        if not filter_name:
+            print "No 'name' set in [%s], skipping." % bucket
+            continue
+        filter_function = config[bucket].get('function', '').strip()
+        if not filter_name:
+            print "No 'function' set in [%s], skipping." % bucket
+            continue
+        yield { 'name': filter_name, 'function': filter_function }
+    raise StopIteration()
+
 def flattened_fallback_configs_for(config):
     children = []
     for child in fallback_configs_for(config):
 #!/usr/bin/env python
 
+import os, sys
 import blatter
+
+sys.path.append(os.getcwd())
+
 blatter.run_script()
-
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.