Skip to content

Commit

Permalink
Add render_string coroutine, make 0.4.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Apr 2, 2015
1 parent 905cea0 commit d18cb8e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
11 changes: 10 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
CHANGES
=======

X.X.X (XXXX-XX-XX)
0.4.0 (2015-04-02)
------------------

- Add `render_string` method

0.3.1 (2015-04-01)
------------------

- Don't allow non-mapping context

- Fix tiny documentation issues

- Change the library logo

0.3.0 (2015-03-15)
------------------

Expand Down
20 changes: 9 additions & 11 deletions aiohttp_jinja2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from aiohttp import web


__version__ = '0.3.1'
__version__ = '0.4.0'

__all__ = ('setup', 'get_env', 'render_template', 'template')

Expand All @@ -23,8 +23,7 @@ def get_env(app, *, app_key=APP_KEY):
return app.get(app_key)


def _render_template(template_name, request, response, context, *,
app_key, encoding):
def render_string(template_name, request, context, *, app_key):
env = request.app.get(app_key)
if env is None:
raise web.HTTPInternalServerError(
Expand All @@ -40,16 +39,16 @@ def _render_template(template_name, request, response, context, *,
raise web.HTTPInternalServerError(
text="context should be mapping, not {}".format(type(context)))
text = template.render(context)
response.content_type = 'text/html'
response.charset = encoding
response.text = text
return text


def render_template(template_name, request, context, *,
app_key=APP_KEY, encoding='utf-8'):
response = web.Response()
_render_template(template_name, request, response, context,
app_key=app_key, encoding=encoding)
text = render_string(template_name, request, context, app_key=app_key)
response.content_type = 'text/html'
response.charset = encoding
response.text = text
return response


Expand All @@ -63,11 +62,10 @@ def wrapped(*args):
coro = func
else:
coro = asyncio.coroutine(func)
response = web.Response()
context = yield from coro(*args)
request = args[-1]
_render_template(template_name, request, response, context,
app_key=app_key, encoding=encoding)
response = render_template(template_name, request, context,
app_key=app_key, encoding=encoding)
response.set_status(status)
return response
return wrapped
Expand Down
19 changes: 17 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,27 @@ Reference
by default.


.. function:: render_string(template_name, request, context, *, \
app_key=APP_KEY)

Return :class:`str` which contains template
*template_name* filled with *context*.

*request* is a parameter from :term:`web-handler`,
:class:`aiohttp.web.Request` instance.

*app_key* is an optional key for application dict, :const:`APP_KEY`
by default.


.. function:: render_template(template_name, request, context, *, \
app_key=APP_KEY, encoding='utf-8')

Return :class:`aiohttp.web.Response` which contains template
*template_name* filled with *context* and is a response to
*request* (:class:`aiohttp.web.Request` instance).
*template_name* filled with *context*.

*request* is a parameter from :term:`web-handler`,
:class:`aiohttp.web.Request` instance.

*app_key* is an optional key for application dict, :const:`APP_KEY`
by default.
Expand Down

0 comments on commit d18cb8e

Please sign in to comment.