added create_jinja_loader
This commit is contained in:
parent
b49de851d9
commit
00c5b7a937
3 changed files with 23 additions and 1 deletions
1
CHANGES
1
CHANGES
|
|
@ -35,6 +35,7 @@ Release date to be announced, codename to be selected
|
||||||
a decorator.
|
a decorator.
|
||||||
- Use Last-Modified for static file sending instead of Date which
|
- Use Last-Modified for static file sending instead of Date which
|
||||||
was incorrectly introduced in 0.6.
|
was incorrectly introduced in 0.6.
|
||||||
|
- Added `create_jinja_loader` to override the loader creation process.
|
||||||
|
|
||||||
Version 0.6.1
|
Version 0.6.1
|
||||||
-------------
|
-------------
|
||||||
|
|
|
||||||
10
flask/app.py
10
flask/app.py
|
|
@ -352,7 +352,15 @@ class Flask(_PackageBoundObject):
|
||||||
options = dict(self.jinja_options)
|
options = dict(self.jinja_options)
|
||||||
if 'autoescape' not in options:
|
if 'autoescape' not in options:
|
||||||
options['autoescape'] = self.select_jinja_autoescape
|
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):
|
def init_jinja_globals(self):
|
||||||
"""Called directly after the environment was created to inject
|
"""Called directly after the environment was created to inject
|
||||||
|
|
|
||||||
|
|
@ -857,6 +857,19 @@ class TemplatingTestCase(unittest.TestCase):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert rv.data == 'dcba'
|
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):
|
class ModuleTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue