2015-08-02 20:59:11 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
flask.templating
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Implements the bridge to Jinja2.
|
|
|
|
|
2016-11-09 19:24:33 +01:00
|
|
|
:copyright: (c) 2015 by Armin Ronacher.
|
2015-08-02 20:59:11 +02:00
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
from jinja2 import BaseLoader, Environment as BaseEnvironment, \
|
|
|
|
TemplateNotFound
|
|
|
|
|
|
|
|
from .globals import _request_ctx_stack, _app_ctx_stack
|
2016-11-09 19:24:33 +01:00
|
|
|
from .signals import template_rendered, before_render_template
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _default_template_ctx_processor():
|
|
|
|
"""Default template context processor. Injects `request`,
|
|
|
|
`session` and `g`.
|
|
|
|
"""
|
|
|
|
reqctx = _request_ctx_stack.top
|
|
|
|
appctx = _app_ctx_stack.top
|
|
|
|
rv = {}
|
|
|
|
if appctx is not None:
|
|
|
|
rv['g'] = appctx.g
|
|
|
|
if reqctx is not None:
|
|
|
|
rv['request'] = reqctx.request
|
|
|
|
rv['session'] = reqctx.session
|
|
|
|
return rv
|
|
|
|
|
|
|
|
|
|
|
|
class Environment(BaseEnvironment):
|
|
|
|
"""Works like a regular Jinja2 environment but has some additional
|
|
|
|
knowledge of how Flask's blueprint works so that it can prepend the
|
|
|
|
name of the blueprint to referenced templates if necessary.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, app, **options):
|
|
|
|
if 'loader' not in options:
|
|
|
|
options['loader'] = app.create_global_jinja_loader()
|
|
|
|
BaseEnvironment.__init__(self, **options)
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
|
|
|
|
class DispatchingJinjaLoader(BaseLoader):
|
|
|
|
"""A loader that looks for templates in the application and all
|
|
|
|
the blueprint folders.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
def get_source(self, environment, template):
|
2016-11-09 19:24:33 +01:00
|
|
|
if self.app.config['EXPLAIN_TEMPLATE_LOADING']:
|
|
|
|
return self._get_source_explained(environment, template)
|
|
|
|
return self._get_source_fast(environment, template)
|
|
|
|
|
|
|
|
def _get_source_explained(self, environment, template):
|
|
|
|
attempts = []
|
|
|
|
trv = None
|
|
|
|
|
|
|
|
for srcobj, loader in self._iter_loaders(template):
|
2015-08-02 20:59:11 +02:00
|
|
|
try:
|
2016-11-09 19:24:33 +01:00
|
|
|
rv = loader.get_source(environment, template)
|
|
|
|
if trv is None:
|
|
|
|
trv = rv
|
2015-08-02 20:59:11 +02:00
|
|
|
except TemplateNotFound:
|
2016-11-09 19:24:33 +01:00
|
|
|
rv = None
|
|
|
|
attempts.append((loader, srcobj, rv))
|
|
|
|
|
|
|
|
from .debughelpers import explain_template_loading_attempts
|
|
|
|
explain_template_loading_attempts(self.app, template, attempts)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-11-09 19:24:33 +01:00
|
|
|
if trv is not None:
|
|
|
|
return trv
|
|
|
|
raise TemplateNotFound(template)
|
|
|
|
|
|
|
|
def _get_source_fast(self, environment, template):
|
|
|
|
for srcobj, loader in self._iter_loaders(template):
|
|
|
|
try:
|
|
|
|
return loader.get_source(environment, template)
|
|
|
|
except TemplateNotFound:
|
|
|
|
continue
|
2015-08-02 20:59:11 +02:00
|
|
|
raise TemplateNotFound(template)
|
|
|
|
|
|
|
|
def _iter_loaders(self, template):
|
|
|
|
loader = self.app.jinja_loader
|
|
|
|
if loader is not None:
|
2016-11-09 19:24:33 +01:00
|
|
|
yield self.app, loader
|
|
|
|
|
|
|
|
for blueprint in self.app.iter_blueprints():
|
2015-08-02 20:59:11 +02:00
|
|
|
loader = blueprint.jinja_loader
|
|
|
|
if loader is not None:
|
2016-11-09 19:24:33 +01:00
|
|
|
yield blueprint, loader
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
def list_templates(self):
|
|
|
|
result = set()
|
|
|
|
loader = self.app.jinja_loader
|
|
|
|
if loader is not None:
|
|
|
|
result.update(loader.list_templates())
|
|
|
|
|
2016-11-09 19:24:33 +01:00
|
|
|
for blueprint in self.app.iter_blueprints():
|
2015-08-02 20:59:11 +02:00
|
|
|
loader = blueprint.jinja_loader
|
|
|
|
if loader is not None:
|
|
|
|
for template in loader.list_templates():
|
2016-11-09 19:24:33 +01:00
|
|
|
result.add(template)
|
2015-08-02 20:59:11 +02:00
|
|
|
|
|
|
|
return list(result)
|
|
|
|
|
|
|
|
|
|
|
|
def _render(template, context, app):
|
|
|
|
"""Renders the template and fires the signal"""
|
2016-11-09 19:24:33 +01:00
|
|
|
|
|
|
|
before_render_template.send(app, template=template, context=context)
|
2015-08-02 20:59:11 +02:00
|
|
|
rv = template.render(context)
|
|
|
|
template_rendered.send(app, template=template, context=context)
|
|
|
|
return rv
|
|
|
|
|
|
|
|
|
|
|
|
def render_template(template_name_or_list, **context):
|
|
|
|
"""Renders a template from the template folder with the given
|
|
|
|
context.
|
|
|
|
|
|
|
|
:param template_name_or_list: the name of the template to be
|
|
|
|
rendered, or an iterable with template names
|
|
|
|
the first one existing will be rendered
|
|
|
|
:param context: the variables that should be available in the
|
|
|
|
context of the template.
|
|
|
|
"""
|
|
|
|
ctx = _app_ctx_stack.top
|
|
|
|
ctx.app.update_template_context(context)
|
|
|
|
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
|
|
|
|
context, ctx.app)
|
|
|
|
|
|
|
|
|
|
|
|
def render_template_string(source, **context):
|
|
|
|
"""Renders a template from the given template source string
|
2016-11-09 19:24:33 +01:00
|
|
|
with the given context. Template variables will be autoescaped.
|
2015-08-02 20:59:11 +02:00
|
|
|
|
2016-11-09 19:24:33 +01:00
|
|
|
:param source: the source code of the template to be
|
2015-08-02 20:59:11 +02:00
|
|
|
rendered
|
|
|
|
:param context: the variables that should be available in the
|
|
|
|
context of the template.
|
|
|
|
"""
|
|
|
|
ctx = _app_ctx_stack.top
|
|
|
|
ctx.app.update_template_context(context)
|
|
|
|
return _render(ctx.app.jinja_env.from_string(source),
|
|
|
|
context, ctx.app)
|