1010 Iterable ,
1111 Mapping ,
1212 Optional ,
13+ Tuple ,
1314 TypeVar ,
1415 Union ,
1516 cast ,
@@ -97,13 +98,12 @@ def get_env(app: web.Application, *, app_key: str = APP_KEY) -> jinja2.Environme
9798 return cast (jinja2 .Environment , app .get (app_key ))
9899
99100
100- def render_string (
101+ def _render_string (
101102 template_name : str ,
102103 request : web .Request ,
103104 context : Mapping [str , Any ],
104- * ,
105- app_key : str = APP_KEY ,
106- ) -> str :
105+ app_key : str ,
106+ ) -> Tuple [jinja2 .Template , Mapping [str , Any ]]:
107107 env = request .config_dict .get (app_key )
108108 if env is None :
109109 text = (
@@ -126,26 +126,71 @@ def render_string(
126126 raise web .HTTPInternalServerError (reason = text , text = text )
127127 if request .get (REQUEST_CONTEXT_KEY ):
128128 context = dict (request [REQUEST_CONTEXT_KEY ], ** context )
129- text = template .render (context )
130- return text
129+ return template , context
131130
132131
133- def render_template (
132+ def render_string (
134133 template_name : str ,
135134 request : web .Request ,
136- context : Optional [ Mapping [str , Any ] ],
135+ context : Mapping [str , Any ],
137136 * ,
138137 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 ]]:
142159 response = web .Response (status = status )
143160 if context is None :
144161 context = {}
145- text = render_string (template_name , request , context , app_key = app_key )
146162 response .content_type = "text/html"
147163 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+ )
149194 return response
150195
151196
@@ -197,9 +242,15 @@ async def wrapped(*args: Any) -> web.StreamResponse:
197242 else :
198243 request = args [- 1 ]
199244
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+ )
203254 response .set_status (status )
204255 return response
205256
0 commit comments