Skip to content

Commit 209f1b2

Browse files
committed
Use coroutines when rendering
1 parent 334f6d4 commit 209f1b2

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

aiohttp_jinja2/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def get_env(app, *, app_key=APP_KEY):
3838
return app.get(app_key)
3939

4040

41+
@asyncio.coroutine
4142
def render_string(template_name, request, context, *, app_key=APP_KEY):
4243
env = request.app.get(app_key)
4344
if env is None:
@@ -59,16 +60,18 @@ def render_string(template_name, request, context, *, app_key=APP_KEY):
5960
raise web.HTTPInternalServerError(reason=text, text=text)
6061
if request.get(REQUEST_CONTEXT_KEY):
6162
context = dict(request[REQUEST_CONTEXT_KEY], **context)
62-
text = template.render(context)
63+
text = yield from template.render(context)
6364
return text
6465

6566

67+
@asyncio.coroutine
6668
def render_template(template_name, request, context, *,
6769
app_key=APP_KEY, encoding='utf-8', status=200):
6870
response = web.Response(status=status)
6971
if context is None:
7072
context = {}
71-
text = render_string(template_name, request, context, app_key=app_key)
73+
text = yield from render_string(template_name, request, context,
74+
app_key=app_key)
7275
response.content_type = 'text/html'
7376
response.charset = encoding
7477
response.text = text
@@ -95,8 +98,9 @@ def wrapped(*args):
9598
else:
9699
request = args[-1]
97100

98-
response = render_template(template_name, request, context,
99-
app_key=app_key, encoding=encoding)
101+
response = yield from render_template(
102+
template_name, request, context,
103+
app_key=app_key, encoding=encoding)
100104
response.set_status(status)
101105
return response
102106
return wrapped

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def aiohttp_app(template):
1313
loader=jinja2.DictLoader({
1414
'tmpl.jinja2': template
1515
}),
16-
enable_async=False,
16+
enable_async=True,
1717
)
1818
return app
1919

tests/test_async_enabled.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
3+
import jinja2
4+
import aiohttp_jinja2
5+
from aiohttp import web
6+
7+
8+
@asyncio.coroutine
9+
def test_func(loop, test_client):
10+
11+
@aiohttp_jinja2.template('tmpl.jinja2')
12+
@asyncio.coroutine
13+
def func(request):
14+
return {'head': 'HEAD', 'text': 'text'}
15+
16+
template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'
17+
18+
app = web.Application(loop=loop)
19+
aiohttp_jinja2.setup(
20+
app,
21+
loader=jinja2.DictLoader({
22+
'tmpl.jinja2': template
23+
}),
24+
)
25+
26+
app.router.add_route('*', '/', func)
27+
28+
client = yield from test_client(app)
29+
30+
resp = yield from client.get('/')
31+
assert 200 == resp.status
32+
txt = yield from resp.text()
33+
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt

0 commit comments

Comments
 (0)