Templates
Air loves Jinja!
A common pattern is to use a Jinja template as the project base and then use Air Tags for individual content.
JinjaRenderer
JinjaRenderer(directory, context_processors=None, env=None)
Template renderer to make Jinja easier in Air.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
str | PathLike[str] | Sequence[str | PathLike[str]]
|
The template directory where Jinja templates for the project are stored. |
required |
context_processors
|
list[Callable[[Request], dict[str, Any]]] | None
|
A list of Jinja-style context processors, functions that automatically injects variables or functions into the template context so they're available in every rendered template without passing them explicitly. |
None
|
env
|
Environment | None
|
The env is the central Jinja object that holds configuration, filters, globals, and template loading settings, and is responsible for compiling and rendering templates. |
None
|
Example:
# Instantiate the render callable
jinja = JinjaRenderer('templates')
# Use for returning Jinja from views
@app.get('/')
async def home(request: Request):
return jinja(
request,
'home.html',
context={'id': 5}
)
# Can also pass in kwargs, which will be added to the context:
return jinja(
request,
'home.html',
name='Parmesan'
)
# Will render Air Tags sent into Jinja context
return jinja(
request,
'home.html',
content=air.Article(air.P('Cheddar'))
)
Source code in src/air/templates.py
55 56 57 58 59 60 61 62 |
|
__call__
__call__(request, name, context=None, **kwargs)
Render template with request and context. If an Air Tag is found in the context, try to render it.
Source code in src/air/templates.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|