Skip to content

Instantly share code, notes, and snippets.

@Pitmairen
Created October 26, 2011 08:04
Show Gist options
  • Save Pitmairen/1315752 to your computer and use it in GitHub Desktop.
Save Pitmairen/1315752 to your computer and use it in GitHub Desktop.
Flask template loader
import os.path
from jinja2 import FileSystemLoader, BaseLoader
from flask import session
class VersionedTemplatesLoader(BaseLoader):
def __init__(self, app):
base_template_path = os.path.join(app.root_path, 'templates')
self._old_templates = FileSystemLoader(
[os.path.join(base_template_path, 'v0')]
)
self._new_templates = FileSystemLoader(
[os.path.join(base_template_path, 'v1')]
)
def get_source(self, environment, template):
if session.get('use_new_templates', False):
return self._new_templates.get_source(environment, template)
return self._old_templates.get_source(environment, template)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment