Use Context Processors for request-dependent variables
masterContext processors allow you to inject variables into every template context. Unlike global variables, context processors can calculate values based on the current request (e.g., the current user).
Note: Context processors follow a 'last-win' strategy, meaning later processors can overwrite variables from earlier ones. aiohttp_jinja2 provides a built-in request_processor which adds the current aiohttp.web.Request object to the template context under the name 'request'.
# Define a processor
async def foo_processor(request):
return {'foo': 'bar'}
# Register it during setup
aiohttp_jinja2.setup(
app,
context_processors=[foo_processor, aiohttp_jinja2.request_processor],
loader=loader)