| 1 |
.. aiohttp_jinja2 documentation master file, created by |
| 2 |
sphinx-quickstart on Sun Mar 22 12:04:15 2015. |
| 3 |
You can adapt this file completely to your liking, but it should at least |
| 4 |
contain the root `toctree` directive. |
| 5 |
|
| 6 |
aiohttp_jinja2 |
| 7 |
============== |
| 8 |
|
| 9 |
.. currentmodule:: aiohttp_jinja2 |
| 10 |
.. highlight:: python |
| 11 |
|
| 12 |
:term:`jinja2` template renderer for :ref:`aiohttp.web<aiohttp-web>`. |
| 13 |
|
| 14 |
|
| 15 |
Usage |
| 16 |
----- |
| 17 |
|
| 18 |
Before template rendering you have to setup *jinja2 environment* |
| 19 |
(:class:`jinja2.Environment`) first:: |
| 20 |
|
| 21 |
app = web.Application() |
| 22 |
aiohttp_jinja2.setup(app, |
| 23 |
loader=jinja2.FileSystemLoader('/path/to/templates/folder')) |
| 24 |
|
| 25 |
|
| 26 |
After that you may use template engine in your |
| 27 |
:term:`web-handlers<web-handler>`. The most convenient way is to |
| 28 |
decorate a :term:`web-handler`. |
| 29 |
|
| 30 |
Using the function based web handlers:: |
| 31 |
|
| 32 |
@aiohttp_jinja2.template('tmpl.jinja2') |
| 33 |
def handler(request): |
| 34 |
return {'name': 'Andrew', 'surname': 'Svetlov'} |
| 35 |
|
| 36 |
Or the class-based views (:class:`aiohttp.web.View`):: |
| 37 |
|
| 38 |
class Handler(web.View): |
| 39 |
@aiohttp_jinja2.template('tmpl.jinja2') |
| 40 |
async def get(self): |
| 41 |
return {'name': 'Andrew', 'surname': 'Svetlov'} |
| 42 |
|
| 43 |
On handler call the :func:`template` decorator will pass |
| 44 |
returned dictionary ``{'name': 'Andrew', 'surname': 'Svetlov'}`` into |
| 45 |
template named ``"tmpl.jinja2"`` for getting resulting HTML text. |
| 46 |
|
| 47 |
More complex template processing can be achieved by modifying the existing |
| 48 |
`list of global functions <http://jinja.pocoo.org/docs/2.10/templates/#builtin-globals>`_. |
| 49 |
Modification of Jinja2's environment can be done via :func:`get_env`. |
| 50 |
For example, adding the ``zip`` function:: |
| 51 |
|
| 52 |
env = aiohttp_jinja2.get_env(app) |
| 53 |
env.globals.update(zip=zip) |
| 54 |
|
| 55 |
|
| 56 |
Which can now to be used in any template:: |
| 57 |
|
| 58 |
{% for value, square in zip(values, squares) %} |
| 59 |
<p>The square of {{ value }} is {{ square }}.</p> |
| 60 |
{% endfor %} |
| 61 |
|
| 62 |
|
| 63 |
In some cases, finer control over the dataflow may also be required. |
| 64 |
This can be worked out by explicitly asking for template to be rendered |
| 65 |
using :func:`render_template`. |
| 66 |
Explicit rendering will allow to possibly |
| 67 |
pass some context to the renderer |
| 68 |
and also to modify its response on the fly. |
| 69 |
This can for example be used to set response headers:: |
| 70 |
|
| 71 |
async def handler(request): |
| 72 |
context = {'name': 'Andrew', 'surname': 'Svetlov'} |
| 73 |
response = aiohttp_jinja2.render_template('tmpl.jinja2', |
| 74 |
request, |
| 75 |
context) |
| 76 |
response.headers['Content-Language'] = 'ru' |
| 77 |
return response |
| 78 |
|
| 79 |
This, again, can also be done with a class-based view (:class:`aiohttp.web.View`):: |
| 80 |
|
| 81 |
class Handler(web.View): |
| 82 |
async def get(self): |
| 83 |
context = {'name': 'Andrew', 'surname': 'Svetlov'} |
| 84 |
response = aiohttp_jinja2.render_template('tmpl.jinja2', |
| 85 |
self.request, |
| 86 |
context) |
| 87 |
response.headers['Content-Language'] = 'ru' |
| 88 |
return response |
| 89 |
|
| 90 |
Context processors is a way to add some variables to each |
| 91 |
template context. It works like :attr:`jinja2.Environment().globals`, |
| 92 |
but calculate variables each request. So if you need to |
| 93 |
add global constants it will be better to use |
| 94 |
:attr:`jinja2.Environment().globals` directly. But if you variables depends of |
| 95 |
request (e.g. current user) you have to use context processors. |
| 96 |
|
| 97 |
Context processors is following last-win strategy. |
| 98 |
Therefore a context processor could rewrite variables delivered with |
| 99 |
previous one. |
| 100 |
|
| 101 |
In order to use context processors create required processors:: |
| 102 |
|
| 103 |
async def foo_processor(request): |
| 104 |
return {'foo': 'bar'} |
| 105 |
|
| 106 |
And pass them into :func:`setup`:: |
| 107 |
|
| 108 |
aiohttp_jinja2.setup( |
| 109 |
app, |
| 110 |
context_processors=[foo_processor, |
| 111 |
aiohttp_jinja2.request_processor], |
| 112 |
loader=loader) |
| 113 |
|
| 114 |
As you can see, there is a built-in :func:`request_processor`, which |
| 115 |
adds current :class:`aiohttp.web.Request` into context of templates |
| 116 |
under ``'request'`` name. |
| 117 |
|
| 118 |
Here is an example of how to add current user dependant logic |
| 119 |
to template (requires ``aiohttp_security`` library):: |
| 120 |
|
| 121 |
from aiohttp_security import authorized_userid |
| 122 |
|
| 123 |
async def current_user_ctx_processor(request): |
| 124 |
userid = await authorized_userid(request) |
| 125 |
is_anonymous = not bool(userid) |
| 126 |
return {'current_user': {'is_anonymous': is_anonymous}} |
| 127 |
|
| 128 |
Template:: |
| 129 |
|
| 130 |
<body> |
| 131 |
<div> |
| 132 |
{% if current_user.is_anonymous %} |
| 133 |
<a href="{{ url('login') }}">Login</a> |
| 134 |
{% else %} |
| 135 |
<a href="{{ url('logout') }}">Logout</a> |
| 136 |
{% endif %} |
| 137 |
</div> |
| 138 |
</body> |
| 139 |
|
| 140 |
Async functions |
| 141 |
............... |
| 142 |
|
| 143 |
If you pass the ``enable_async`` parameter to the setup function, then you |
| 144 |
will need to use the async functions for rendering:: |
| 145 |
|
| 146 |
aiohttp_jinja2.setup( |
| 147 |
app, enable_async=True, |
| 148 |
loader=jinja2.FileSystemLoader('/path/to/templates/folder')) |
| 149 |
|
| 150 |
... |
| 151 |
|
| 152 |
async def handler(request): |
| 153 |
return await aiohttp_jinja2.render_template_async( |
| 154 |
'tmpl.jinja2', request) |
| 155 |
|
| 156 |
The ``@aiohttp_jinja2.template`` decorator will work for both cases. |
| 157 |
|
| 158 |
Default Globals |
| 159 |
............... |
| 160 |
|
| 161 |
.. highlight:: html+jinja |
| 162 |
|
| 163 |
``app`` is always made in templates via :attr:`jinja2.Environment().globals`:: |
| 164 |
|
| 165 |
<body> |
| 166 |
<h1>Welcome to {{ app['name'] }}</h1> |
| 167 |
</body> |
| 168 |
|
| 169 |
|
| 170 |
Two more helpers are also enabled by default: ``url`` and ``static``. |
| 171 |
|
| 172 |
``url`` can be used with just a view name:: |
| 173 |
|
| 174 |
<body> |
| 175 |
<a href="{{ url('index') }}">Index Page</a> |
| 176 |
</body> |
| 177 |
|
| 178 |
|
| 179 |
Or with arguments:: |
| 180 |
|
| 181 |
<body> |
| 182 |
<a href="{{ url('user', id=123) }}">User Page</a> |
| 183 |
</body> |
| 184 |
|
| 185 |
A query can be added to the url with the special ``query_`` keyword argument:: |
| 186 |
|
| 187 |
<body> |
| 188 |
<a href="{{ url('user', id=123, query_={'foo': 'bar'}) }}">User Page</a> |
| 189 |
</body> |
| 190 |
|
| 191 |
|
| 192 |
For a view defined by ``app.router.add_get('/user-profile/{id}/', |
| 193 |
user, name='user')``, the above would give:: |
| 194 |
|
| 195 |
<body> |
| 196 |
<a href="/user-profile/123/?foo=bar">User Page</a> |
| 197 |
</body> |
| 198 |
|
| 199 |
|
| 200 |
This is useful as it would allow your static path to switch in |
| 201 |
deployment or testing with just one line. |
| 202 |
|
| 203 |
The ``static`` function has similar usage, except it requires you to |
| 204 |
set ``app[aiohttp_jinja2.static_root_key]``. |
| 205 |
|
| 206 |
.. code-block:: python |
| 207 |
|
| 208 |
app = web.Application() |
| 209 |
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader("/path/to/templates/folder")) |
| 210 |
app[aiohttp_jinja2.static_root_key] = "/static" |
| 211 |
|
| 212 |
Then in the template:: |
| 213 |
|
| 214 |
<script src="{{ static('dist/main.js') }}"></script> |
| 215 |
|
| 216 |
|
| 217 |
Would result in:: |
| 218 |
|
| 219 |
<script src="/static/dist/main.js"></script> |
| 220 |
|
| 221 |
|
| 222 |
Both ``url`` and ``static`` can be disabled by passing |
| 223 |
``default_helpers=False`` to ``aiohttp_jinja2.setup``. |
| 224 |
|
| 225 |
Library Installation |
| 226 |
-------------------- |
| 227 |
|
| 228 |
The :mod:`aiohttp_jinja2` can be installed by pip:: |
| 229 |
|
| 230 |
$ pip3 install aiohttp_jinja2 |
| 231 |
|
| 232 |
Source code |
| 233 |
----------- |
| 234 |
|
| 235 |
The project is hosted on `GitHub <https://github.com/aio-libs/aiohttp_jinja2>`_. |
| 236 |
|
| 237 |
Please feel free to file an issue on `bug tracker |
| 238 |
<https://github.com/aio-libs/aiohttp_jinja2/issues>`_ if you have found a bug |
| 239 |
or have some suggestion for library improvement. |
| 240 |
|
| 241 |
The library uses `Travis <https://travis-ci.org/aio-libs/aiohttp-jinja2>`_ for |
| 242 |
Continuous Integration. |
| 243 |
|
| 244 |
|
| 245 |
License |
| 246 |
------- |
| 247 |
|
| 248 |
:mod:`aiohttp_jinja2` is offered under the Apache 2 license. |
| 249 |
|
| 250 |
Contents |
| 251 |
-------- |
| 252 |
|
| 253 |
.. toctree:: |
| 254 |
:maxdepth: 2 |
| 255 |
|
| 256 |
api |
| 257 |
|
| 258 |
|
| 259 |
Indices and tables |
| 260 |
------------------ |
| 261 |
|
| 262 |
* :ref:`genindex` |
| 263 |
* :ref:`search` |
| 264 |
|
| 265 |
|
| 266 |
Glossary |
| 267 |
-------- |
| 268 |
|
| 269 |
.. if you add new entries, keep the alphabetical sorting! |
| 270 |
|
| 271 |
.. glossary:: |
| 272 |
|
| 273 |
jinja2 |
| 274 |
|
| 275 |
A modern and designer-friendly templating language for Python. |
| 276 |
|
| 277 |
See http://jinja.pocoo.org/ |
| 278 |
|
| 279 |
web-handler |
| 280 |
|
| 281 |
An endpoint that returns http response. |