-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mako rendering example and link to examples in docs
- Loading branch information
Showing
3 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"""Example of using content negotiation to support HTML representations, using | ||
Mako for templating. Also demonstrates app configuration. | ||
Start the app with | ||
:: | ||
$ pip install mako | ||
$ python examples/mako_example.py | ||
Try it out: | ||
:: | ||
$ pip install httpie | ||
$ http :8000/ Accept:application/json | ||
$ http :8000/ Accept:text/html | ||
""" | ||
from collections import OrderedDict, Mapping | ||
from asyncio import coroutine | ||
|
||
from aiohttp import web | ||
from aiohttp_utils import Response, negotiation, run | ||
|
||
from mako.lookup import TemplateLookup | ||
|
||
##### Templates ##### | ||
|
||
lookup = TemplateLookup() | ||
# Note: In a real app, this would be in a file. | ||
template = """ | ||
<html> | ||
<body> | ||
<h1>${message}</h1> | ||
</body> | ||
</html> | ||
""" | ||
lookup.put_string('index.html', template) | ||
|
||
##### Handlers ##### | ||
|
||
@coroutine | ||
def index(request): | ||
return Response({ | ||
'message': 'Hello ' + request.GET.get('name', 'World') | ||
}) | ||
|
||
##### Custom router ##### | ||
|
||
class RoutingWithTemplating(web.UrlDispatcher): | ||
"""Optionally save a template name on a handler function's __dict__.""" | ||
|
||
def add_route(self, method, path, handler, template: str=None, **kwargs): | ||
if template: | ||
handler.__dict__['template'] = template | ||
super().add_route(method, path, handler, **kwargs) | ||
|
||
##### Renderer ##### | ||
|
||
def render_mako(request, data, handler): | ||
template_name = handler.__dict__.get('template', None) | ||
if not template_name: | ||
raise web.HTTPNotAcceptable(text='text/html not supported.') | ||
if not isinstance(data, Mapping): | ||
raise web.HTTPInternalServerError( | ||
text="context should be mapping, not {}".format(type(data))) | ||
template = request.app['mako_lookup'].get_template(template_name) | ||
text = template.render_unicode(**data) | ||
return web.Response(text=text, content_type=request['selected_media_type']) | ||
|
||
##### Configuration ##### | ||
|
||
CONFIG = { | ||
'AIOHTTP_UTILS': { | ||
'RENDERERS': OrderedDict([ | ||
('application/json', negotiation.render_json), | ||
('text/html', render_mako), | ||
]) | ||
} | ||
} | ||
|
||
##### Application ##### | ||
|
||
app = web.Application(router=RoutingWithTemplating(), debug=True) | ||
app['mako_lookup'] = lookup | ||
app.update(CONFIG) | ||
negotiation.setup(app) | ||
app.router.add_route('GET', '/', index, template='index.html') | ||
|
||
if __name__ == "__main__": | ||
run( | ||
app, | ||
app_uri='examples.mako_example:app', | ||
reload=True, | ||
port=8000, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters