Skip to content

Commit

Permalink
Added autoloading of custom jinja filters under package.lib.templatet…
Browse files Browse the repository at this point in the history
…ools.jinja_filters
  • Loading branch information
clsdaniel authored and amol- committed Oct 18, 2011
1 parent 8356371 commit 94175a8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tg/configuration.py
Expand Up @@ -437,16 +437,25 @@ def setup_jinja_renderer(self):
if not 'jinja_extensions' in self :
self.jinja_extensions = []

# TODO: Load jinja filters automatically from given modules
if not 'jinja_filters' in self:
self.jinja_filters = {}

config['pylons.app_globals'].jinja2_env = Environment(loader=ChoiceLoader(
[FileSystemLoader(path) for path in self.paths['templates']]),
auto_reload=self.auto_reload_templates, extensions=self.jinja_extensions)

# Try to load custom filters module under app_package.lib.templatetools
try:
filter_package = self.package.__name__ + ".lib.templatetools"
autoload_lib = __import__(filter_package, {}, {}, ['jinja_filters'])
autoload_filters = autoload_lib.jinja_filters.__dict__
except (ImportError, AttributeError):
autoload_filters = {}

# Add jinja filters
config['pylons.app_globals'].jinja2_env.filters = dict(FILTERS, **self.jinja_filters)
filters = dict(FILTERS, **autoload_filters)
filters.update(self.jinja_filters)
config['pylons.app_globals'].jinja2_env.filters = filters

# Jinja's unable to request c's attributes without strict_c
warnings.simplefilter("ignore")
Expand Down

0 comments on commit 94175a8

Please sign in to comment.