| 1 |
API |
| 2 |
=== |
| 3 |
|
| 4 |
Describes the module API with detailed explanations of functions parameters. |
| 5 |
|
| 6 |
.. module:: aiohttp_jinja2 |
| 7 |
.. highlight:: python |
| 8 |
|
| 9 |
|
| 10 |
APP_KEY |
| 11 |
------- |
| 12 |
|
| 13 |
.. data:: APP_KEY |
| 14 |
|
| 15 |
The key name in :class:`aiohttp.web.Application` dictionary, |
| 16 |
``'aiohttp_jinja2_environment'`` for storing :term:`jinja2` |
| 17 |
environment object (:class:`jinja2.Environment`). |
| 18 |
|
| 19 |
Usually you don't need to operate with *application* manually, left |
| 20 |
it to :mod:`aiohttp_jinja2` functions. |
| 21 |
|
| 22 |
|
| 23 |
setup |
| 24 |
----- |
| 25 |
|
| 26 |
.. function:: setup(app, *args, app_key=APP_KEY, context_processors=(), \ |
| 27 |
autoescape=True, \ |
| 28 |
filters=None, default_helpers=True, **kwargs) |
| 29 |
|
| 30 |
Function responsible for initializing templating system on application. It |
| 31 |
must be called before freezing or running the application in order to use |
| 32 |
*aiohttp-jinja*. |
| 33 |
|
| 34 |
:param app: :class:`aiohttp.web.Application` instance to initialize template |
| 35 |
system on. |
| 36 |
|
| 37 |
:param str app_key: optional key that will be used to access templating |
| 38 |
environment from application dictionary object. Defaults |
| 39 |
to `aiohttp_jinja2_environment`. |
| 40 |
|
| 41 |
:param context_processors: list of :ref:`aiohttp-web-middlewares`. These are |
| 42 |
context processors used to rewrite or inject some |
| 43 |
variables during the processing of a request. |
| 44 |
:type context_processors: :class:`list` |
| 45 |
|
| 46 |
:param autoescape: the argument is passed to :class:`jinja2.Environemnt`, see |
| 47 |
`Autoescaping` for more details. |
| 48 |
|
| 49 |
:param filters: extra jinja filters (`link to docs |
| 50 |
<http://jinja.pocoo.org/docs/2.10/templates/#filters>`). |
| 51 |
:type filters: :class:`list` |
| 52 |
|
| 53 |
:param bool default_helpers: whether to use default global helper in |
| 54 |
templates provided by package |
| 55 |
:mod:`aiohttp_jinja2.helpers` or not. |
| 56 |
|
| 57 |
:param ``*args``: positional arguments passed into environment constructor. |
| 58 |
:param ``**kwargs``: any arbitrary keyword arguments you want to pass to |
| 59 |
:class:`jinja2.Environment` environment. |
| 60 |
|
| 61 |
Simple initialization:: |
| 62 |
|
| 63 |
import jinja2 |
| 64 |
import aiohttp_jinja2 |
| 65 |
from aiohttp import web |
| 66 |
|
| 67 |
app = web.Application() |
| 68 |
aiohttp_jinja2.setup( |
| 69 |
app, |
| 70 |
loader=jinja2.FileSystemLoader('/path/to/templates/folder'), |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
@template |
| 75 |
-------- |
| 76 |
|
| 77 |
.. decorator:: template(template_name, *, app_key=APP_KEY, \ |
| 78 |
encoding='utf-8', status=200) |
| 79 |
|
| 80 |
Behaves as a decorator around view functions accepting template name that |
| 81 |
should be used to render the response. Supports both synchronous and |
| 82 |
asynchronous functions. |
| 83 |
|
| 84 |
:param str template_name: name of the template file that will be looked up |
| 85 |
by the loader. Raises a 500 error in case template |
| 86 |
was not found. |
| 87 |
|
| 88 |
:param str app_key: optional key that will be used to access templating |
| 89 |
environment from application dictionary object. Defaults |
| 90 |
to `aiohttp_jinja2_environment`. |
| 91 |
|
| 92 |
:param str encoding: encoding that will be set as a charset property on the |
| 93 |
response for rendered template, default to utf-8. |
| 94 |
|
| 95 |
:params int status: http status code that will be set on resulting response. |
| 96 |
|
| 97 |
|
| 98 |
Simple usage example:: |
| 99 |
|
| 100 |
@jinja2.template('tmpl.jinja2') |
| 101 |
async def handler(request): |
| 102 |
context = {'foo': 'bar'} |
| 103 |
return context |
| 104 |
|
| 105 |
app.router.add_get('/tmpl', handler) |
| 106 |
|
| 107 |
render_string |
| 108 |
------------- |
| 109 |
|
| 110 |
.. function:: render_string(template_name, request, context, *, \ |
| 111 |
app_key=APP_KEY) |
| 112 |
|
| 113 |
Renders template specified and returns resulting string. |
| 114 |
|
| 115 |
:param str template_name: Name of the template you want to render. Usually |
| 116 |
it's a filename without extension on your |
| 117 |
filesystem. |
| 118 |
:param request: aiohttp request associated with an application where |
| 119 |
aiohttp-jinja rendering is configured. |
| 120 |
:type request: :class:`aiohttp.web.Request` |
| 121 |
|
| 122 |
:param dict context: dictionary used as context when rendering the template. |
| 123 |
:param str app_key: optional key that will be used to access templating |
| 124 |
environment from application dictionary object. Defaults |
| 125 |
to `aiohttp_jinja2_environment`. |
| 126 |
|
| 127 |
|
| 128 |
render_string_async |
| 129 |
------------------- |
| 130 |
|
| 131 |
.. function:: render_string_async(template_name, request, context, *, \ |
| 132 |
app_key=APP_KEY) |
| 133 |
:async: |
| 134 |
|
| 135 |
Async version of ``render_string()``. |
| 136 |
|
| 137 |
Replaces ``render_string()`` when ``enable_async=True`` is passed to the |
| 138 |
``setup()`` call. |
| 139 |
|
| 140 |
See ``render_string()`` for parameter usage. |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
render_template |
| 145 |
--------------- |
| 146 |
|
| 147 |
.. function:: render_template(template_name, request, context, *, \ |
| 148 |
app_key=APP_KEY, encoding='utf-8', status=200) |
| 149 |
|
| 150 |
:param str template_name: Name of the template you want to render. |
| 151 |
:param request: aiohttp request associated with an application where |
| 152 |
aiohttp-jinja rendering is configured. |
| 153 |
:type request: :class:`aiohttp.web.Request` |
| 154 |
|
| 155 |
:param dict context: dictionary used as context when rendering the template. |
| 156 |
:param str app_key: optional key that will be used to access templating |
| 157 |
environment from application dictionary object. Defaults |
| 158 |
to `aiohttp_jinja2_environment`. |
| 159 |
:param int status: http status code that will be set on resulting response. |
| 160 |
|
| 161 |
Assuming the initialization from the example above has been done:: |
| 162 |
|
| 163 |
async def handler(request): |
| 164 |
context = {'foo': 'bar'} |
| 165 |
response = aiohttp_jinja2.render_template('tmpl.jinja2', |
| 166 |
request, |
| 167 |
context) |
| 168 |
return response |
| 169 |
|
| 170 |
app.router.add_get('/tmpl', handler) |
| 171 |
|
| 172 |
|
| 173 |
render_template_async |
| 174 |
--------------------- |
| 175 |
|
| 176 |
.. function:: render_template_async( \ |
| 177 |
template_name, request, context, *, \ |
| 178 |
app_key=APP_KEY, encoding='utf-8', status=200) |
| 179 |
:async: |
| 180 |
|
| 181 |
Async version of ``render_template()``. |
| 182 |
|
| 183 |
Replaces ``render_template()`` when ``enable_async=True`` is passed to the |
| 184 |
``setup()`` call. |
| 185 |
|
| 186 |
See ``render_template()`` for parameter usage. |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
.. function:: get_env(app, *, app_key=APP_KEY) |
| 191 |
|
| 192 |
Get aiohttp-jinja2 environment from an application instance by key. |
| 193 |
|
| 194 |
:param app: :class:`aiohttp.web.Application` instance to get variables from. |
| 195 |
|
| 196 |
:param str app_key: optional key that will be used to access templating |
| 197 |
environment from application dictionary object. Defaults |
| 198 |
to `aiohttp_jinja2_environment`. |