diff --git a/CHANGES b/CHANGES index 2ca6e355..9127c126 100644 --- a/CHANGES +++ b/CHANGES @@ -35,6 +35,7 @@ Release date to be announced, codename to be selected a decorator. - Use Last-Modified for static file sending instead of Date which was incorrectly introduced in 0.6. +- Added `create_jinja_loader` to override the loader creation process. Version 0.6.1 ------------- diff --git a/flask/app.py b/flask/app.py index 39445ab3..c247422a 100644 --- a/flask/app.py +++ b/flask/app.py @@ -352,7 +352,15 @@ class Flask(_PackageBoundObject): options = dict(self.jinja_options) if 'autoescape' not in options: options['autoescape'] = self.select_jinja_autoescape - return Environment(loader=_DispatchingJinjaLoader(self), **options) + return Environment(loader=self.create_jinja_loader(), **options) + + def create_jinja_loader(self): + """Creates the loader for the Jinja2 environment. Can be used to + override just the loader and keeping the rest unchanged. + + .. versionadded:: 0.7 + """ + return _DispatchingJinjaLoader(self) def init_jinja_globals(self): """Called directly after the environment was created to inject diff --git a/tests/flask_tests.py b/tests/flask_tests.py index db2616a9..04b54777 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -857,6 +857,19 @@ class TemplatingTestCase(unittest.TestCase): rv = app.test_client().get('/') assert rv.data == 'dcba' + def test_custom_template_loader(self): + class MyFlask(flask.Flask): + def create_jinja_loader(self): + from jinja2 import DictLoader + return DictLoader({'index.html': 'Hello Custom World!'}) + app = MyFlask(__name__) + @app.route('/') + def index(): + return flask.render_template('index.html') + c = app.test_client() + rv = c.get('/') + assert rv.data == 'Hello Custom World!' + class ModuleTestCase(unittest.TestCase):