| 1 |
aiohttp_jinja2 |
| 2 |
============== |
| 3 |
.. image:: https://github.com/aio-libs/aiohttp-jinja2/workflows/CI/badge.svg |
| 4 |
:target: https://github.com/aio-libs/aiohttp-jinja2/actions?query=workflow%3ACI |
| 5 |
.. image:: https://codecov.io/gh/aio-libs/aiohttp-jinja2/branch/master/graph/badge.svg |
| 6 |
:target: https://codecov.io/gh/aio-libs/aiohttp-jinja2 |
| 7 |
.. image:: https://img.shields.io/pypi/v/aiohttp-jinja2.svg |
| 8 |
:target: https://pypi.python.org/pypi/aiohttp-jinja2 |
| 9 |
.. image:: https://readthedocs.org/projects/aiohttp-jinja2/badge/?version=latest |
| 10 |
:target: http://aiohttp-jinja2.readthedocs.io/en/latest/?badge=latest |
| 11 |
|
| 12 |
|
| 13 |
jinja2_ template renderer for `aiohttp.web`__. |
| 14 |
|
| 15 |
|
| 16 |
.. _jinja2: http://jinja.pocoo.org |
| 17 |
|
| 18 |
.. _aiohttp_web: https://aiohttp.readthedocs.io/en/latest/web.html |
| 19 |
|
| 20 |
__ aiohttp_web_ |
| 21 |
|
| 22 |
Installation |
| 23 |
------------ |
| 24 |
Install from PyPI:: |
| 25 |
|
| 26 |
pip install aiohttp-jinja2 |
| 27 |
|
| 28 |
|
| 29 |
Developing |
| 30 |
---------- |
| 31 |
|
| 32 |
Install requirement and launch tests:: |
| 33 |
|
| 34 |
pip install -r requirements-dev.txt |
| 35 |
pytest tests |
| 36 |
|
| 37 |
|
| 38 |
Usage |
| 39 |
----- |
| 40 |
|
| 41 |
Before template rendering you have to setup *jinja2 environment* first: |
| 42 |
|
| 43 |
.. code-block:: python |
| 44 |
|
| 45 |
app = web.Application() |
| 46 |
aiohttp_jinja2.setup(app, |
| 47 |
loader=jinja2.FileSystemLoader('/path/to/templates/folder')) |
| 48 |
|
| 49 |
Import: |
| 50 |
|
| 51 |
.. code-block:: python |
| 52 |
|
| 53 |
import aiohttp_jinja2 |
| 54 |
import jinja2 |
| 55 |
|
| 56 |
After that you may to use template engine in your *web-handlers*. The |
| 57 |
most convenient way is to decorate a *web-handler*. |
| 58 |
|
| 59 |
Using the function based web handlers: |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
@aiohttp_jinja2.template('tmpl.jinja2') |
| 64 |
def handler(request): |
| 65 |
return {'name': 'Andrew', 'surname': 'Svetlov'} |
| 66 |
|
| 67 |
Or for `Class Based Views |
| 68 |
<https://aiohttp.readthedocs.io/en/stable/web_quickstart.html#class-based-views>`: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
class Handler(web.View): |
| 73 |
@aiohttp_jinja2.template('tmpl.jinja2') |
| 74 |
async def get(self): |
| 75 |
return {'name': 'Andrew', 'surname': 'Svetlov'} |
| 76 |
|
| 77 |
|
| 78 |
On handler call the ``aiohttp_jinja2.template`` decorator will pass |
| 79 |
returned dictionary ``{'name': 'Andrew', 'surname': 'Svetlov'}`` into |
| 80 |
template named ``tmpl.jinja2`` for getting resulting HTML text. |
| 81 |
|
| 82 |
If you need more complex processing (set response headers for example) |
| 83 |
you may call ``render_template`` function. |
| 84 |
|
| 85 |
Using a function based web handler: |
| 86 |
|
| 87 |
.. code-block:: python |
| 88 |
|
| 89 |
async def handler(request): |
| 90 |
context = {'name': 'Andrew', 'surname': 'Svetlov'} |
| 91 |
response = aiohttp_jinja2.render_template('tmpl.jinja2', |
| 92 |
request, |
| 93 |
context) |
| 94 |
response.headers['Content-Language'] = 'ru' |
| 95 |
return response |
| 96 |
|
| 97 |
Or, again, a class based view: |
| 98 |
|
| 99 |
.. code-block:: python |
| 100 |
|
| 101 |
class Handler(web.View): |
| 102 |
async def get(self): |
| 103 |
context = {'name': 'Andrew', 'surname': 'Svetlov'} |
| 104 |
response = aiohttp_jinja2.render_template('tmpl.jinja2', |
| 105 |
self.request, |
| 106 |
context) |
| 107 |
response.headers['Content-Language'] = 'ru' |
| 108 |
return response |
| 109 |
|
| 110 |
|
| 111 |
License |
| 112 |
------- |
| 113 |
|
| 114 |
``aiohttp_jinja2`` is offered under the Apache 2 license. |