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
Provenance
- Commit
a3c9494- Tree
6b69ca1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a38dcd5
4 files changed+79 added−62 removed
modified:flask/__init__.py+2 added-2 removed
16 unmodified lines17 18 from flask.app import Flask, Request, Response19 from flask.config import Config20 from 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, json22 from flask.globals import current_app, g, request, session, _request_ctx_stack23 from flask.module import Module24+from flask.templating import render_template, render_template_stringmodified:flask/app.py+3 added-35 removed
12 unmodified lines13 from threading import Lock14 from datetime import timedelta, datetime15 from itertools import chain16 17-from jinja2 import Environment, BaseLoader, FileSystemLoader, TemplateNotFound17+from jinja2 import Environment18+19 from werkzeug import ImmutableDict, create_environ20 from werkzeug.routing import Map, Rule21 from werkzeug.exceptions import HTTPException, InternalServerError, NotFound22 4 unmodified lines27 from flask.ctx import _default_template_ctx_processor, _RequestContext28 from flask.globals import _request_ctx_stack, request29 from flask.session import Session, _NullSession30 from flask.module import _ModuleSetupState31+from flask.templating import _DispatchingJinjaLoader32 33 # a lock used for logger initialization34 _logger_lock = Lock()35 36 35-class _DispatchingJinjaLoader(BaseLoader):36- """A loader that looks for templates in the application and all37- the module folders.38- """39-40- def __init__(self, app):41- self.app = app42-43- 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)59-60- 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 result67-68-37 class 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 for794 unmodified linesmodified:flask/helpers.py-25 removed
288 unmodified lines289 rv.headers.pop('x-sendfile', None)290 return rv291 292 293-def render_template(template_name, **context):294- """Renders a template from the template folder with the given295- context.296-297- :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)303-304-305-def render_template_string(source, **context):306- """Renders a template from the given template source string307- with the given context.308-309- :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)316-317-293 def _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 linesadded:flask/templating.py+74 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.templating4+ ~~~~~~~~~~~~~~~~5+6+ Implements the bridge to Jinja2.7+8+ :copyright: (c) 2010 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound12+13+from flask.globals import _request_ctx_stack14+15+16+class _DispatchingJinjaLoader(BaseLoader):17+ """A loader that looks for templates in the application and all18+ the module folders.19+ """20+21+ def __init__(self, app):22+ self.app = app23+24+ 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)40+41+ 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 result48+49+50+def render_template(template_name, **context):51+ """Renders a template from the template folder with the given52+ context.53+54+ :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)61+62+63+def render_template_string(source, **context):64+ """Renders a template from the given template source string65+ with the given context.66+67+ :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)