Browse Source

isolate templates between blueprints

pull/1963/head
LingchuanHu 9 years ago
parent
commit
a9b3f17e7f
  1. 3
      flask/templating.py
  2. 5
      tests/test_apps/blueprintapp/apps/admin/__init__.py
  3. 1
      tests/test_apps/blueprintapp/apps/admin/templates/index.html
  4. 5
      tests/test_apps/blueprintapp/apps/frontend/__init__.py
  5. 1
      tests/test_apps/blueprintapp/apps/frontend/templates/index.html
  6. 9
      tests/test_templating.py

3
flask/templating.py

@ -89,7 +89,10 @@ class DispatchingJinjaLoader(BaseLoader):
if loader is not None:
yield self.app, loader
blueprint_name = _request_ctx_stack.top.request.blueprint
for blueprint in self.app.iter_blueprints():
if blueprint.name != blueprint_name:
continue
loader = blueprint.jinja_loader
if loader is not None:
yield blueprint, loader

5
tests/test_apps/blueprintapp/apps/admin/__init__.py

@ -10,6 +10,11 @@ def index():
return render_template('admin/index.html')
@admin.route('/admin')
def admin_control():
return render_template('index.html')
@admin.route('/index2')
def index2():
return render_template('./admin/index.html')

1
tests/test_apps/blueprintapp/apps/admin/templates/index.html

@ -0,0 +1 @@
admin

5
tests/test_apps/blueprintapp/apps/frontend/__init__.py

@ -8,6 +8,11 @@ def index():
return render_template('frontend/index.html')
@frontend.route('/frontend')
def frontend_control():
return render_template('index.html')
@frontend.route('/missing')
def missing_template():
return render_template('missing_template.html')

1
tests/test_apps/blueprintapp/apps/frontend/templates/index.html

@ -0,0 +1 @@
frontend

9
tests/test_templating.py

@ -390,3 +390,12 @@ def test_custom_jinja_env():
app = CustomFlask(__name__)
assert isinstance(app.jinja_env, CustomEnvironment)
def test_blueprint_template_isolation(test_apps):
from blueprintapp import app
rv1 = app.test_client().get('/admin/admin')
rv2 = app.test_client().get('/frontend')
assert rv1.data == b'admin'
assert rv2.data == b'frontend'

Loading…
Cancel
Save