Jinja
This extension provides support for Jinja templates.
Usage
To use jinja templates, first install the jinja
extra:
Then activate the extension in the configuration file:
To render templates, inject the selva.ext.templates.jinja.JinjaTemplate
dependency
and call its respond
method:
from typing import Annotated
from selva.di import Inject
from selva.ext.templates.jinja import JinjaTemplate
from selva.web import get
@get
async def index(request, template: Annotated[JinjaTemplate, Inject]):
context = {"title": "Index"}
await template.respond(request.response, "index.html", context)
Render templates to string
The JinjaTemplate
class provide methods to render templates into a str
, instead
of rendering to the response.
The method JinjaTempate.render
accepts a template name and returns a string with the
rendered template.
The method JinjaTempate.render_str
accepts a template string, compiles it and returns
the result.
rendered = template.render("template.html", {"variable": "value"})
rendered = template.render_str("{{ variable }}", {"variable": "value"})
Configuration
Jinja can be configured through the settings.yaml
. For example, to activate Jinja extensions:
Full list of settings:
templates:
jinja:
paths:
- resources/templates
block_start_string: ""
block_end_string: ""
variable_start_string: ""
variable_end_string: ""
comment_start_string: ""
comment_end_string: ""
line_statement_prefix: ""
line_comment_prefix: ""
trim_blocks: true
lstrip_blocks: true
newline_sequence: "\n" # or "\r\n" or "\r"
keep_trailing_newline: true
extensions:
- extension1
- extensions2
optimized: true
# dotted path to python class
undefined: "package.module.Class"
# dotted path to python function
finalize: "package.module.function"
# dotted path to python function
autoescape: "package.module.function"
# dotted path to python variable
loader: "package.module:variable"
cache_size: 1
auto_reload: true
# dotted path to python variable
bytecode_cache: "package.module:variable"