# -*- coding: utf-8 -*-"""flask.templating~~~~~~~~~~~~~~~~Implements the bridge to Jinja2.:copyright: © 2010 by the Pallets team.:license: BSD, see LICENSE for more details."""from jinja2 import BaseLoader, Environment as BaseEnvironment, \TemplateNotFoundfrom .globals import _request_ctx_stack, _app_ctx_stackfrom .signals import template_rendered, before_render_templatedef _default_template_ctx_processor():"""Default template context processor. Injects `request`,`session` and `g`."""reqctx = _request_ctx_stack.topappctx = _app_ctx_stack.toprv = {}if appctx is not None:rv['g'] = appctx.gif reqctx is not None:rv['request'] = reqctx.requestrv['session'] = reqctx.sessionreturn rvclass Environment(BaseEnvironment):"""Works like a regular Jinja2 environment but has some additionalknowledge of how Flask's blueprint works so that it can prepend thename 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 = appclass DispatchingJinjaLoader(BaseLoader):"""A loader that looks for templates in the application and allthe blueprint folders."""def __init__(self, app):self.app = appdef get_source(self, environment, template):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 = Nonefor srcobj, loader in self._iter_loaders(template):try:rv = loader.get_source(environment, template)if trv is None:trv = rvexcept TemplateNotFound:rv = Noneattempts.append((loader, srcobj, rv))from .debughelpers import explain_template_loading_attemptsexplain_template_loading_attempts(self.app, template, attempts)if trv is not None:return trvraise 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:continueraise TemplateNotFound(template)def _iter_loaders(self, template):loader = self.app.jinja_loaderif loader is not None:yield self.app, loaderfor blueprint in self.app.iter_blueprints():loader = blueprint.jinja_loaderif loader is not None:yield blueprint, loaderdef list_templates(self):result = set()loader = self.app.jinja_loaderif loader is not None:result.update(loader.list_templates())for blueprint in self.app.iter_blueprints():loader = blueprint.jinja_loaderif loader is not None:for template in loader.list_templates():result.add(template)return list(result)def _render(template, context, app):"""Renders the template and fires the signal"""before_render_template.send(app, template=template, context=context)rv = template.render(context)template_rendered.send(app, template=template, context=context)return rvdef render_template(template_name_or_list, **context):"""Renders a template from the template folder with the givencontext.:param template_name_or_list: the name of the template to berendered, or an iterable with template namesthe first one existing will be rendered:param context: the variables that should be available in thecontext of the template."""ctx = _app_ctx_stack.topctx.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 stringwith the given context. Template variables will be autoescaped.:param source: the source code of the template to berendered:param context: the variables that should be available in thecontext of the template."""ctx = _app_ctx_stack.topctx.app.update_template_context(context)return _render(ctx.app.jinja_env.from_string(source),context, ctx.app)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。