Commits

Augie Fackler committed 2e6d60d

Add ability to define custom Jinja extensions.

  • Participants
  • Parent commits 6bdc389

Comments (0)

Files changed (1)

blatter/__init__.py

 dynamic_dir=site
 output_dir=out
 
+# add custom extensions if desired:
+# extensions=dotted.path.to.ExtensionClass,extensions.OtherExtension
+
 # filter options can be used to import custom Jinja filters
 # 'name' is what the filter will be called by in Jinja tags
 # 'function' is the (module) path to the function.
                 for fallback in flattened_fallback_configs_for(config))
 
     loader = jinja2.loaders.ChoiceLoader(path)
-    env = jinja2.Environment(loader=loader)
+    extensions = []
+    for extra_extension in config.get('extensions', '').split(','):
+        fullpath = extra_extension.strip()
+        if not fullpath or '.' not in fullpath:
+            print 'Invalid extension path', fullpath, 'skipping.'
+            continue
+        mod_name, cls_name = fullpath.rsplit('.', 1)
+        mod_path = ''
+        if '.' in mod_name:
+            mod_path, mod_name = mod_name.rsplit('.', 1)
+        mod = __import__(mod_name, fromlist=[mod_path])
+        extensions.append(getattr(mod, cls_name))
+    env = jinja2.Environment(loader=loader, extensions=extensions)
     for extra_filter in filter_configs_for(config):
         f = extra_filter['function']
         f_module_path = '.'.join(f.split('.')[:-2])