Separation between setting up the template env and the template loader

for a glorious future where we have gettext in template context
This commit is contained in:
Christopher Allan Webber 2011-05-12 15:17:07 -05:00
parent f62ccaac15
commit 0e0e3d9aad
2 changed files with 19 additions and 8 deletions

View File

@ -40,7 +40,7 @@ class MediaGoblinApp(object):
email_sender_address, email_debug_mode, email_sender_address, email_debug_mode,
user_template_path=None): user_template_path=None):
# Get the template environment # Get the template environment
self.template_env = util.get_jinja_env(user_template_path) self.template_loader = util.get_jinja_loader(user_template_path)
# Set up storage systems # Set up storage systems
self.public_store = public_store self.public_store = public_store
@ -103,7 +103,10 @@ class MediaGoblinApp(object):
# Attach self as request.app # Attach self as request.app
# Also attach a few utilities from request.app for convenience? # Also attach a few utilities from request.app for convenience?
request.app = self request.app = self
request.template_env = self.template_env request.locale = util.get_locale_from_request(request)
request.template_env = util.get_jinja_env(
self.template_loader, request.locale)
request.db = self.db request.db = self.db
request.staticdirect = self.staticdirector request.staticdirect = self.staticdirector

View File

@ -33,23 +33,31 @@ def _activate_testing():
TESTS_ENABLED = True TESTS_ENABLED = True
def get_jinja_env(user_template_path=None): def get_jinja_loader(user_template_path=None):
""" """
Set up the Jinja environment, possibly allowing for user Set up the Jinja template loaders, possibly allowing for user
overridden templates. overridden templates.
(In the future we may have another system for providing theming; (In the future we may have another system for providing theming;
for now this is good enough.) for now this is good enough.)
""" """
if user_template_path: if user_template_path:
loader = jinja2.ChoiceLoader( return jinja2.ChoiceLoader(
[jinja2.FileSystemLoader(user_template_path), [jinja2.FileSystemLoader(user_template_path),
jinja2.PackageLoader('mediagoblin', 'templates')]) jinja2.PackageLoader('mediagoblin', 'templates')])
else: else:
loader = jinja2.PackageLoader('mediagoblin', 'templates') return jinja2.PackageLoader('mediagoblin', 'templates')
def get_jinja_env(template_loader, locale):
"""
Set up the Jinja environment,
(In the future we may have another system for providing theming;
for now this is good enough.)
"""
return jinja2.Environment( return jinja2.Environment(
loader=loader, autoescape=True, loader=template_loader, autoescape=True,
extensions=['jinja2.ext.i18n']) extensions=['jinja2.ext.i18n'])
@ -237,4 +245,4 @@ def get_locale_from_request(request):
else: else:
target_lang = 'en' target_lang = 'en'
return make_locale_lower_upper_style(target_lang) return locale_to_lower_upper(target_lang)