Skip to content
Myr

Moved templating stuff into a separate module

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent a38dcd5 · Browse files · 4 files+79 added−62 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+79 added−62 removed

  • other:+79 added−62 removed

4 files+79 added−62 removed

modified:flask/__init__.py+2 added-2 removed
16 unmodified lines
1718from flask.app import Flask, Request, Response19from flask.config import Config20from flask.helpers import url_for, jsonify, json_available, flash, send_file, \21 get_flashed_messages, render_template, render_template, render_template_string, \22 get_template_attribute, json21 get_flashed_messages, get_template_attribute, json22from flask.globals import current_app, g, request, session, _request_ctx_stack23from flask.module import Module24from flask.templating import render_template, render_template_string
modified:flask/app.py+3 added-35 removed
12 unmodified lines
13from threading import Lock14from datetime import timedelta, datetime15from itertools import chain1617from jinja2 import Environment, BaseLoader, FileSystemLoader, TemplateNotFound17from jinja2 import Environment1819from werkzeug import ImmutableDict, create_environ20from werkzeug.routing import Map, Rule21from werkzeug.exceptions import HTTPException, InternalServerError, NotFound22
4 unmodified lines
27from flask.ctx import _default_template_ctx_processor, _RequestContext28from flask.globals import _request_ctx_stack, request29from flask.session import Session, _NullSession30from flask.module import _ModuleSetupState31from flask.templating import _DispatchingJinjaLoader3233# a lock used for logger initialization34_logger_lock = Lock()353635class _DispatchingJinjaLoader(BaseLoader):36 """A loader that looks for templates in the application and all37 the module folders.38 """3940 def __init__(self, app):41 self.app = app4243 def get_source(self, environment, template):44 name = template45 loader = None46 try:47 module, name = template.split('/', 1)48 loader = self.app.modules[module].jinja_loader49 except (ValueError, KeyError):50 pass51 if loader is None:52 loader = self.app.jinja_loader53 try:54 return loader.get_source(environment, name)55 except TemplateNotFound:56 # re-raise the exception with the correct fileame here.57 # (the one that includes the prefix)58 raise TemplateNotFound(template)5960 def list_templates(self):61 result = self.app.jinja_loader.list_templates()62 for name, module in self.app.modules.iteritems():63 if module.jinja_loader is not None:64 for template in module.jinja_loader.list_templates():65 result.append('%s/%s' % (name, template))66 return result676837class Flask(_PackageBoundObject):38 """The flask object implements a WSGI application and acts as the central39 object. It is passed the name of the module or package of the40 application. Once it is created it will act as a central registry for
794 unmodified lines
modified:flask/helpers.py-25 removed
288 unmodified lines
289 rv.headers.pop('x-sendfile', None)290 return rv291292293def render_template(template_name, **context):294 """Renders a template from the template folder with the given295 context.296297 :param template_name: the name of the template to be rendered298 :param context: the variables that should be available in the299 context of the template.300 """301 current_app.update_template_context(context)302 return current_app.jinja_env.get_template(template_name).render(context)303304305def render_template_string(source, **context):306 """Renders a template from the given template source string307 with the given context.308309 :param template_name: the sourcecode of the template to be310 rendered311 :param context: the variables that should be available in the312 context of the template.313 """314 current_app.update_template_context(context)315 return current_app.jinja_env.from_string(source).render(context)316317293def _get_package_path(name):294 """Returns the path to a package or cwd if that cannot be found."""295 try:296 return os.path.abspath(os.path.dirname(sys.modules[name].__file__))
70 unmodified lines
added:flask/templating.py+74 added
1# -*- coding: utf-8 -*-2"""3 flask.templating4 ~~~~~~~~~~~~~~~~56 Implements the bridge to Jinja2.78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound1213from flask.globals import _request_ctx_stack141516class _DispatchingJinjaLoader(BaseLoader):17 """A loader that looks for templates in the application and all18 the module folders.19 """2021 def __init__(self, app):22 self.app = app2324 def get_source(self, environment, template):25 name = template26 loader = None27 try:28 module, name = template.split('/', 1)29 loader = self.app.modules[module].jinja_loader30 except (ValueError, KeyError):31 pass32 if loader is None:33 loader = self.app.jinja_loader34 try:35 return loader.get_source(environment, name)36 except TemplateNotFound:37 # re-raise the exception with the correct fileame here.38 # (the one that includes the prefix)39 raise TemplateNotFound(template)4041 def list_templates(self):42 result = self.app.jinja_loader.list_templates()43 for name, module in self.app.modules.iteritems():44 if module.jinja_loader is not None:45 for template in module.jinja_loader.list_templates():46 result.append('%s/%s' % (name, template))47 return result484950def render_template(template_name, **context):51 """Renders a template from the template folder with the given52 context.5354 :param template_name: the name of the template to be rendered55 :param context: the variables that should be available in the56 context of the template.57 """58 ctx = _request_ctx_stack.top59 ctx.app.update_template_context(context)60 return ctx.app.jinja_env.get_template(template_name).render(context)616263def render_template_string(source, **context):64 """Renders a template from the given template source string65 with the given context.6667 :param template_name: the sourcecode of the template to be68 rendered69 :param context: the variables that should be available in the70 context of the template.71 """72 ctx = _request_ctx_stack.top73 ctx.app.update_template_context(context)74 return ctx.app.jinja_env.from_string(source).render(context)