-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathtemplating.py
164 lines (133 loc) · 5.17 KB
/
templating.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from request_handling import WebMessageHandler
###
### Mako templates
###
def load_mako_env(template_dir, *args, **kwargs):
"""Returns a function which loads a Mako templates environment.
"""
def loader():
from mako.lookup import TemplateLookup
if template_dir is not None:
return TemplateLookup(directories=[template_dir or '.'],
*args, **kwargs)
else:
return None
return loader
class MakoRendering(WebMessageHandler):
def render_template(self, template_file,
_status_code=WebMessageHandler._SUCCESS_CODE,
**context):
body = self.application.render_template(template_file, **context or {})
self.set_body(body, status_code=_status_code)
return self.render()
def render_error(self, error_code):
return self.render_template('errors.html', _status_code=error_code,
**{'error_code': error_code})
###
### Jinja2
###
def load_jinja2_env(template_dir, *args, **kwargs):
"""Returns a function that loads a jinja template environment. Uses a
closure to provide a namespace around module loading without loading
anything until the caller is ready.
"""
def loader():
from jinja2 import Environment, FileSystemLoader
if template_dir is not None:
return Environment(loader=FileSystemLoader(template_dir or '.'),
*args, **kwargs)
else:
return None
return loader
class Jinja2Rendering(WebMessageHandler):
"""Jinja2Rendering is a mixin for for loading a Jinja2 rendering
environment.
Render success is transmitted via http 200. Rendering failures result in
http 500 errors.
"""
def render_template(self, template_file,
_status_code=WebMessageHandler._SUCCESS_CODE,
**context):
"""Renders payload as a jinja template
"""
body = self.application.render_template(template_file, **context or {})
self.set_body(body, status_code=_status_code)
return self.render()
def render_error(self, error_code):
"""Receives error calls and sends them through a templated renderer
call.
"""
return self.render_template('errors.html', _status_code=error_code,
**{'error_code': error_code})
###
### Tornado
###
def load_tornado_env(template_dir, *args, **kwargs):
"""Returns a function that loads the Tornado template environment.
"""
def loader():
from tornado.template import Loader
if template_dir is not None:
return Loader(template_dir or '.', *args, **kwargs)
else:
return None
return loader
class TornadoRendering(WebMessageHandler):
"""TornadoRendering is a mixin for for loading a Tornado rendering
environment.
Follows usual convention: 200 => success and 500 => failure
The unusual convention of an underscore in front of a variable is used
to avoid conflict with **context. '_status_code', for now, is a reserved
word.
"""
def render_template(self, template_file,
_status_code=WebMessageHandler._SUCCESS_CODE,
**context):
"""Renders payload as a tornado template
"""
body = self.application.render_template(template_file, **context or {})
self.set_body(body, status_code=_status_code)
return self.render()
def render_error(self, error_code):
"""Receives error calls and sends them through a templated renderer
call.
"""
return self.render_template('errors.html', _status_code=error_code,
**{'error_code': error_code})
###
### Mustache
###
def load_mustache_env(template_dir, *args, **kwargs):
"""
Returns a function that loads a mustache template environment. Uses a
closure to provide a namespace around module loading without loading
anything until the caller is ready.
"""
def loader():
import pystache
return pystache.Renderer(search_dirs=[template_dir])
return loader
class MustacheRendering(WebMessageHandler):
"""
MustacheRendering is a mixin for for loading a Mustache rendering
environment.
Render success is transmitted via http 200. Rendering failures result in
http 500 errors.
"""
def render_template(self, template_file,
_status_code=WebMessageHandler._SUCCESS_CODE,
**context):
"""
Renders payload as a mustache template
"""
mustache_env = self.application.template_env
template = mustache_env.load_template(template_file)
body = mustache_env.render(template, context or {})
self.set_body(body, status_code=_status_code)
return self.render()
def render_error(self, error_code):
"""Receives error calls and sends them through a templated renderer
call.
"""
return self.render_template('errors', _status_code=error_code,
**{'error_code': error_code})