Skip to content

Commit 491af32

Browse files
committed
Allow launching in blocking mode with enable_async=False flag
1 parent efc1128 commit 491af32

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

aiohttp_jinja2/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ def render_string(template_name, request, context, *, app_key=APP_KEY):
6060
raise web.HTTPInternalServerError(reason=text, text=text)
6161
if request.get(REQUEST_CONTEXT_KEY):
6262
context = dict(request[REQUEST_CONTEXT_KEY], **context)
63-
text = yield from template.render_async(context)
63+
if env.is_async:
64+
text = yield from template.render_async(context)
65+
else:
66+
text = template.render(context)
6467
return text
6568

6669

tests/test_async_enabled.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/test_async_mode.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import asyncio
2+
3+
import jinja2
4+
import aiohttp_jinja2
5+
from aiohttp import web
6+
7+
8+
@asyncio.coroutine
9+
def test_async_mode_disabled(test_client, loop):
10+
"""
11+
Test that running in blocking manner will also work.
12+
"""
13+
@aiohttp_jinja2.template('tmpl.jinja2')
14+
@asyncio.coroutine
15+
def func(request):
16+
return {'foo': 42}
17+
18+
app = web.Application(loop=loop)
19+
template = "foo: {{foo}}"
20+
aiohttp_jinja2.setup(
21+
app,
22+
loader=jinja2.DictLoader({
23+
'tmpl.jinja2': template,
24+
}),
25+
enable_async=False,
26+
)
27+
28+
app.router.add_get('/', func)
29+
30+
client = yield from test_client(app)
31+
32+
resp = yield from client.get('/')
33+
assert 200 == resp.status
34+
txt = yield from resp.text()
35+
assert 'foo: 42' == txt

0 commit comments

Comments
 (0)