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