55from aiohttp import web
66
77
8- __version__ = '0.3.1 '
8+ __version__ = '0.4.0 '
99
1010__all__ = ('setup' , 'get_env' , 'render_template' , 'template' )
1111
@@ -23,8 +23,7 @@ def get_env(app, *, app_key=APP_KEY):
2323 return app .get (app_key )
2424
2525
26- def _render_template (template_name , request , response , context , * ,
27- app_key , encoding ):
26+ def render_string (template_name , request , context , * , app_key ):
2827 env = request .app .get (app_key )
2928 if env is None :
3029 raise web .HTTPInternalServerError (
@@ -40,16 +39,16 @@ def _render_template(template_name, request, response, context, *,
4039 raise web .HTTPInternalServerError (
4140 text = "context should be mapping, not {}" .format (type (context )))
4241 text = template .render (context )
43- response .content_type = 'text/html'
44- response .charset = encoding
45- response .text = text
42+ return text
4643
4744
4845def render_template (template_name , request , context , * ,
4946 app_key = APP_KEY , encoding = 'utf-8' ):
5047 response = web .Response ()
51- _render_template (template_name , request , response , context ,
52- app_key = app_key , encoding = encoding )
48+ text = render_string (template_name , request , context , app_key = app_key )
49+ response .content_type = 'text/html'
50+ response .charset = encoding
51+ response .text = text
5352 return response
5453
5554
@@ -63,11 +62,10 @@ def wrapped(*args):
6362 coro = func
6463 else :
6564 coro = asyncio .coroutine (func )
66- response = web .Response ()
6765 context = yield from coro (* args )
6866 request = args [- 1 ]
69- _render_template (template_name , request , response , context ,
70- app_key = app_key , encoding = encoding )
67+ response = render_template (template_name , request , context ,
68+ app_key = app_key , encoding = encoding )
7169 response .set_status (status )
7270 return response
7371 return wrapped
0 commit comments