10
10
Iterable ,
11
11
Mapping ,
12
12
Optional ,
13
+ Tuple ,
13
14
TypeVar ,
14
15
Union ,
15
16
cast ,
@@ -97,13 +98,12 @@ def get_env(app: web.Application, *, app_key: str = APP_KEY) -> jinja2.Environme
97
98
return cast (jinja2 .Environment , app .get (app_key ))
98
99
99
100
100
- def render_string (
101
+ def _render_string (
101
102
template_name : str ,
102
103
request : web .Request ,
103
104
context : Mapping [str , Any ],
104
- * ,
105
- app_key : str = APP_KEY ,
106
- ) -> str :
105
+ app_key : str ,
106
+ ) -> Tuple [jinja2 .Template , Mapping [str , Any ]]:
107
107
env = request .config_dict .get (app_key )
108
108
if env is None :
109
109
text = (
@@ -126,26 +126,71 @@ def render_string(
126
126
raise web .HTTPInternalServerError (reason = text , text = text )
127
127
if request .get (REQUEST_CONTEXT_KEY ):
128
128
context = dict (request [REQUEST_CONTEXT_KEY ], ** context )
129
- text = template .render (context )
130
- return text
129
+ return template , context
131
130
132
131
133
- def render_template (
132
+ def render_string (
134
133
template_name : str ,
135
134
request : web .Request ,
136
- context : Optional [ Mapping [str , Any ] ],
135
+ context : Mapping [str , Any ],
137
136
* ,
138
137
app_key : str = APP_KEY ,
139
- encoding : str = "utf-8" ,
140
- status : int = 200 ,
141
- ) -> web .Response :
138
+ ) -> str :
139
+ template , context = _render_string (template_name , request , context , app_key )
140
+ return template .render (context )
141
+
142
+
143
+ async def render_string_async (
144
+ template_name : str ,
145
+ request : web .Request ,
146
+ context : Mapping [str , Any ],
147
+ * ,
148
+ app_key : str = APP_KEY ,
149
+ ) -> str :
150
+ template , context = _render_string (template_name , request , context , app_key )
151
+ return await template .render_async (context )
152
+
153
+
154
+ def _render_template (
155
+ context : Optional [Mapping [str , Any ]],
156
+ encoding : str ,
157
+ status : int ,
158
+ ) -> Tuple [web .Response , Mapping [str , Any ]]:
142
159
response = web .Response (status = status )
143
160
if context is None :
144
161
context = {}
145
- text = render_string (template_name , request , context , app_key = app_key )
146
162
response .content_type = "text/html"
147
163
response .charset = encoding
148
- response .text = text
164
+ return response , context
165
+
166
+
167
+ def render_template (
168
+ template_name : str ,
169
+ request : web .Request ,
170
+ context : Optional [Mapping [str , Any ]],
171
+ * ,
172
+ app_key : str = APP_KEY ,
173
+ encoding : str = "utf-8" ,
174
+ status : int = 200 ,
175
+ ) -> web .Response :
176
+ response , context = _render_template (context , encoding , status )
177
+ response .text = render_string (template_name , request , context , app_key = app_key )
178
+ return response
179
+
180
+
181
+ async def render_template_async (
182
+ template_name : str ,
183
+ request : web .Request ,
184
+ context : Optional [Mapping [str , Any ]],
185
+ * ,
186
+ app_key : str = APP_KEY ,
187
+ encoding : str = "utf-8" ,
188
+ status : int = 200 ,
189
+ ) -> web .Response :
190
+ response , context = _render_template (context , encoding , status )
191
+ response .text = await render_string_async (
192
+ template_name , request , context , app_key = app_key
193
+ )
149
194
return response
150
195
151
196
@@ -197,9 +242,15 @@ async def wrapped(*args: Any) -> web.StreamResponse:
197
242
else :
198
243
request = args [- 1 ]
199
244
200
- response = render_template (
201
- template_name , request , context , app_key = app_key , encoding = encoding
202
- )
245
+ env = request .config_dict .get (app_key )
246
+ if env and env .is_async :
247
+ response = await render_template_async (
248
+ template_name , request , context , app_key = app_key , encoding = encoding
249
+ )
250
+ else :
251
+ response = render_template (
252
+ template_name , request , context , app_key = app_key , encoding = encoding
253
+ )
203
254
response .set_status (status )
204
255
return response
205
256
0 commit comments