Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 97 lines (78 sloc) 3.056 kb
a3c9494 @mitsuhiko Moved templating stuff into a separate module
authored
1 # -*- coding: utf-8 -*-
2 """
3 flask.templating
4 ~~~~~~~~~~~~~~~~
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, TemplateNotFound
12
80eb6cf @mitsuhiko Switched to relative imports in the package
authored
13 from .globals import _request_ctx_stack
e0712b4 @mitsuhiko Added support for signals
authored
14 from .signals import template_rendered
a3c9494 @mitsuhiko Moved templating stuff into a separate module
authored
15
16
665fa2a @mitsuhiko More refactoring and moving stuff around
authored
17 def _default_template_ctx_processor():
18 """Default template context processor. Injects `request`,
19 `session` and `g`.
20 """
21 reqctx = _request_ctx_stack.top
22 return dict(
aa3d839 @mitsuhiko Config is now available in templates, context processors no longer ov…
authored
23 config=reqctx.app.config,
665fa2a @mitsuhiko More refactoring and moving stuff around
authored
24 request=reqctx.request,
25 session=reqctx.session,
26 g=reqctx.g
27 )
28
29
a3c9494 @mitsuhiko Moved templating stuff into a separate module
authored
30 class _DispatchingJinjaLoader(BaseLoader):
31 """A loader that looks for templates in the application and all
32 the module folders.
33 """
34
35 def __init__(self, app):
36 self.app = app
37
38 def get_source(self, environment, template):
1202996 @mitsuhiko Fixed a template loading bug
authored
39 loader = None
a3c9494 @mitsuhiko Moved templating stuff into a separate module
authored
40 try:
41 module, name = template.split('/', 1)
42 loader = self.app.modules[module].jinja_loader
43 except (ValueError, KeyError):
1202996 @mitsuhiko Fixed a template loading bug
authored
44 pass
45 # if there was a module and it has a loader, try this first
46 if loader is not None:
0a93c55 @mitsuhiko Fixed a template lookup error
authored
47 try:
48 return loader.get_source(environment, name)
49 except TemplateNotFound:
50 pass
1202996 @mitsuhiko Fixed a template loading bug
authored
51 # fall back to application loader if module failed
52 return self.app.jinja_loader.get_source(environment, template)
a3c9494 @mitsuhiko Moved templating stuff into a separate module
authored
53
54 def list_templates(self):
55 result = self.app.jinja_loader.list_templates()
56 for name, module in self.app.modules.iteritems():
57 if module.jinja_loader is not None:
58 for template in module.jinja_loader.list_templates():
59 result.append('%s/%s' % (name, template))
60 return result
61
62
e0712b4 @mitsuhiko Added support for signals
authored
63 def _render(template, context, app):
64 """Renders the template and fires the signal"""
65 rv = template.render(context)
66 template_rendered.send(app, template=template, context=context)
67 return rv
68
69
a3c9494 @mitsuhiko Moved templating stuff into a separate module
authored
70 def render_template(template_name, **context):
71 """Renders a template from the template folder with the given
72 context.
73
74 :param template_name: the name of the template to be rendered
75 :param context: the variables that should be available in the
76 context of the template.
77 """
78 ctx = _request_ctx_stack.top
79 ctx.app.update_template_context(context)
e0712b4 @mitsuhiko Added support for signals
authored
80 return _render(ctx.app.jinja_env.get_template(template_name),
81 context, ctx.app)
a3c9494 @mitsuhiko Moved templating stuff into a separate module
authored
82
83
84 def render_template_string(source, **context):
85 """Renders a template from the given template source string
86 with the given context.
87
88 :param template_name: the sourcecode of the template to be
89 rendered
90 :param context: the variables that should be available in the
91 context of the template.
92 """
93 ctx = _request_ctx_stack.top
94 ctx.app.update_template_context(context)
e0712b4 @mitsuhiko Added support for signals
authored
95 return _render(ctx.app.jinja_env.from_string(source),
96 context, ctx.app)
Something went wrong with that request. Please try again.