forked from orbit-oss/flask
Moved templating stuff into a separate module
This commit is contained in:
parent
a38dcd5e2b
commit
a3c9494f67
4 changed files with 79 additions and 62 deletions
|
|
@ -18,7 +18,7 @@ from jinja2 import Markup, escape
|
||||||
from flask.app import Flask, Request, Response
|
from flask.app import Flask, Request, Response
|
||||||
from flask.config import Config
|
from flask.config import Config
|
||||||
from flask.helpers import url_for, jsonify, json_available, flash, send_file, \
|
from flask.helpers import url_for, jsonify, json_available, flash, send_file, \
|
||||||
get_flashed_messages, render_template, render_template, render_template_string, \
|
get_flashed_messages, get_template_attribute, json
|
||||||
get_template_attribute, json
|
|
||||||
from flask.globals import current_app, g, request, session, _request_ctx_stack
|
from flask.globals import current_app, g, request, session, _request_ctx_stack
|
||||||
from flask.module import Module
|
from flask.module import Module
|
||||||
|
from flask.templating import render_template, render_template_string
|
||||||
|
|
|
||||||
38
flask/app.py
38
flask/app.py
|
|
@ -14,7 +14,8 @@ from threading import Lock
|
||||||
from datetime import timedelta, datetime
|
from datetime import timedelta, datetime
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from jinja2 import Environment, BaseLoader, FileSystemLoader, TemplateNotFound
|
from jinja2 import Environment
|
||||||
|
|
||||||
from werkzeug import ImmutableDict, create_environ
|
from werkzeug import ImmutableDict, create_environ
|
||||||
from werkzeug.routing import Map, Rule
|
from werkzeug.routing import Map, Rule
|
||||||
from werkzeug.exceptions import HTTPException, InternalServerError, NotFound
|
from werkzeug.exceptions import HTTPException, InternalServerError, NotFound
|
||||||
|
|
@ -27,45 +28,12 @@ from flask.ctx import _default_template_ctx_processor, _RequestContext
|
||||||
from flask.globals import _request_ctx_stack, request
|
from flask.globals import _request_ctx_stack, request
|
||||||
from flask.session import Session, _NullSession
|
from flask.session import Session, _NullSession
|
||||||
from flask.module import _ModuleSetupState
|
from flask.module import _ModuleSetupState
|
||||||
|
from flask.templating import _DispatchingJinjaLoader
|
||||||
|
|
||||||
# a lock used for logger initialization
|
# a lock used for logger initialization
|
||||||
_logger_lock = Lock()
|
_logger_lock = Lock()
|
||||||
|
|
||||||
|
|
||||||
class _DispatchingJinjaLoader(BaseLoader):
|
|
||||||
"""A loader that looks for templates in the application and all
|
|
||||||
the module folders.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, app):
|
|
||||||
self.app = app
|
|
||||||
|
|
||||||
def get_source(self, environment, template):
|
|
||||||
name = template
|
|
||||||
loader = None
|
|
||||||
try:
|
|
||||||
module, name = template.split('/', 1)
|
|
||||||
loader = self.app.modules[module].jinja_loader
|
|
||||||
except (ValueError, KeyError):
|
|
||||||
pass
|
|
||||||
if loader is None:
|
|
||||||
loader = self.app.jinja_loader
|
|
||||||
try:
|
|
||||||
return loader.get_source(environment, name)
|
|
||||||
except TemplateNotFound:
|
|
||||||
# re-raise the exception with the correct fileame here.
|
|
||||||
# (the one that includes the prefix)
|
|
||||||
raise TemplateNotFound(template)
|
|
||||||
|
|
||||||
def list_templates(self):
|
|
||||||
result = self.app.jinja_loader.list_templates()
|
|
||||||
for name, module in self.app.modules.iteritems():
|
|
||||||
if module.jinja_loader is not None:
|
|
||||||
for template in module.jinja_loader.list_templates():
|
|
||||||
result.append('%s/%s' % (name, template))
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
class Flask(_PackageBoundObject):
|
class Flask(_PackageBoundObject):
|
||||||
"""The flask object implements a WSGI application and acts as the central
|
"""The flask object implements a WSGI application and acts as the central
|
||||||
object. It is passed the name of the module or package of the
|
object. It is passed the name of the module or package of the
|
||||||
|
|
|
||||||
|
|
@ -290,31 +290,6 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
|
|
||||||
def render_template(template_name, **context):
|
|
||||||
"""Renders a template from the template folder with the given
|
|
||||||
context.
|
|
||||||
|
|
||||||
:param template_name: the name of the template to be rendered
|
|
||||||
:param context: the variables that should be available in the
|
|
||||||
context of the template.
|
|
||||||
"""
|
|
||||||
current_app.update_template_context(context)
|
|
||||||
return current_app.jinja_env.get_template(template_name).render(context)
|
|
||||||
|
|
||||||
|
|
||||||
def render_template_string(source, **context):
|
|
||||||
"""Renders a template from the given template source string
|
|
||||||
with the given context.
|
|
||||||
|
|
||||||
:param template_name: the sourcecode of the template to be
|
|
||||||
rendered
|
|
||||||
:param context: the variables that should be available in the
|
|
||||||
context of the template.
|
|
||||||
"""
|
|
||||||
current_app.update_template_context(context)
|
|
||||||
return current_app.jinja_env.from_string(source).render(context)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_package_path(name):
|
def _get_package_path(name):
|
||||||
"""Returns the path to a package or cwd if that cannot be found."""
|
"""Returns the path to a package or cwd if that cannot be found."""
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
74
flask/templating.py
Normal file
74
flask/templating.py
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
flask.templating
|
||||||
|
~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Implements the bridge to Jinja2.
|
||||||
|
|
||||||
|
:copyright: (c) 2010 by Armin Ronacher.
|
||||||
|
:license: BSD, see LICENSE for more details.
|
||||||
|
"""
|
||||||
|
from jinja2 import BaseLoader, FileSystemLoader, TemplateNotFound
|
||||||
|
|
||||||
|
from flask.globals import _request_ctx_stack
|
||||||
|
|
||||||
|
|
||||||
|
class _DispatchingJinjaLoader(BaseLoader):
|
||||||
|
"""A loader that looks for templates in the application and all
|
||||||
|
the module folders.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, app):
|
||||||
|
self.app = app
|
||||||
|
|
||||||
|
def get_source(self, environment, template):
|
||||||
|
name = template
|
||||||
|
loader = None
|
||||||
|
try:
|
||||||
|
module, name = template.split('/', 1)
|
||||||
|
loader = self.app.modules[module].jinja_loader
|
||||||
|
except (ValueError, KeyError):
|
||||||
|
pass
|
||||||
|
if loader is None:
|
||||||
|
loader = self.app.jinja_loader
|
||||||
|
try:
|
||||||
|
return loader.get_source(environment, name)
|
||||||
|
except TemplateNotFound:
|
||||||
|
# re-raise the exception with the correct fileame here.
|
||||||
|
# (the one that includes the prefix)
|
||||||
|
raise TemplateNotFound(template)
|
||||||
|
|
||||||
|
def list_templates(self):
|
||||||
|
result = self.app.jinja_loader.list_templates()
|
||||||
|
for name, module in self.app.modules.iteritems():
|
||||||
|
if module.jinja_loader is not None:
|
||||||
|
for template in module.jinja_loader.list_templates():
|
||||||
|
result.append('%s/%s' % (name, template))
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def render_template(template_name, **context):
|
||||||
|
"""Renders a template from the template folder with the given
|
||||||
|
context.
|
||||||
|
|
||||||
|
:param template_name: the name of the template to be rendered
|
||||||
|
:param context: the variables that should be available in the
|
||||||
|
context of the template.
|
||||||
|
"""
|
||||||
|
ctx = _request_ctx_stack.top
|
||||||
|
ctx.app.update_template_context(context)
|
||||||
|
return ctx.app.jinja_env.get_template(template_name).render(context)
|
||||||
|
|
||||||
|
|
||||||
|
def render_template_string(source, **context):
|
||||||
|
"""Renders a template from the given template source string
|
||||||
|
with the given context.
|
||||||
|
|
||||||
|
:param template_name: the sourcecode of the template to be
|
||||||
|
rendered
|
||||||
|
:param context: the variables that should be available in the
|
||||||
|
context of the template.
|
||||||
|
"""
|
||||||
|
ctx = _request_ctx_stack.top
|
||||||
|
ctx.app.update_template_context(context)
|
||||||
|
return ctx.app.jinja_env.from_string(source).render(context)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue