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