.. aiohttp_mako documentation master file, created by sphinx-quickstart on Sun Mar 22 12:04:15 2015. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. aiohttp_mako ============ mako_ template renderer for `aiohttp.web`__. `aiohttp_mako` based on :term:`aiohttp_jinja2`. Library has almost same api and it is used in aiohttp_debugtoolbar_. .. _mako: http://www.makotemplates.org/ .. _aiohttp_debugtoolbar: https://github.com/aio-libs/aiohttp_debugtoolbar .. _aiohttp_web: http://aiohttp.readthedocs.org/en/latest/web.html __ aiohttp_web_ Usage ----- Before template rendering you have to setup *mako* templates location first:: app = web.Application(loop=loop) lookup = aiohttp_mako.setup(app, input_encoding='utf-8', output_encoding='utf-8', default_filters=['decode.utf8']) After that you may to use template engine in your *web-handlers*. The most convinient way is to decorate *web-handler*:: @aiohttp_mako.template('tmpl.html') async def handler(request): return {'head': 'aiohttp_mako', 'text': 'Hello World!'} On handler call the ``aiohttp_mako.template`` decorator will pass returned dictionary ``{'head': 'aiohttp_mako', 'text': 'Hello World!'}`` into template named ``"tmpl.html"`` for getting resulting HTML text. If you need more complex processing (set response headers for example) you may call ``render_template`` function:: async def handler(request): context = {'head': 'aiohttp_mako', 'text': 'Hello World!'} response = aiohttp_mako.render_template('tmpl.html', request, context) response.headers['Content-Language'] = 'ru' return response .. _aiohttp_mako-reference: Context processors ~~~~~~~~~~~~~~~~~~ Context processors is a way to add some variables to each template context. It calculates variables on each request. Context processors is following last-win strategy. Therefore a context processor could rewrite variables delivered with previous one. In order to use context processors create required processors:: async def foo_processor(request): return {'foo': 'bar'} And pass them into :func:`setup`:: aiohttp_mako.setup( app, context_processors=[foo_processor, aiohttp_mako.request_processor], loader=loader) As you can see, there is a built-in :func:`request_processor`, which adds current :class:`aiohttp.web.Request` into context of templates under ``'request'`` name. Example ------- :: import asyncio import aiohttp_mako from aiohttp import web @aiohttp_mako.template('index.html') def func(request): return {'head': 'aiohttp_mako', 'text': 'Hello World!'} async def init(loop): app = web.Application(loop=loop) lookup = aiohttp_mako.setup(app, input_encoding='utf-8', output_encoding='utf-8', default_filters=['decode.utf8']) template = """