Litestar API
The Litestar integration provides htmx-aware template rendering with block support via the HTMXBlockTemplate class.
API Reference
- exception jinja2_fragments.litestar.BlockNotFoundError(block_name, template_name, message=None)[source]
Bases:
LitestarException
- class jinja2_fragments.litestar.HTMXBlockTemplate(*args, push_url=None, re_swap=None, re_target=None, trigger_event=None, params=None, after=None, block_name=None, block_names=None, **kwargs)[source]
Bases:
HTMXTemplateA class for rendering specific template blocks with support for HTMX.
This class facilitates the rendering of individual template blocks or a set of blocks, particularly for HTMX responses. It provides methods for block-specific rendering and conversion of the result into an ASGI-compatible response.
- block_name
The name of the single block to be rendered. Optional if block_names is provided.
- block_names
A list of block names to be rendered. Optional if block_name is provided.
- __init__(*args, push_url=None, re_swap=None, re_target=None, trigger_event=None, params=None, after=None, block_name=None, block_names=None, **kwargs)[source]
Initialize the HTMXBlockTemplate instance.
The constructor sets up the initial configuration for block rendering. You can specify either a single block (block_name) or multiple blocks (block_names), but not both at the same time.
- Parameters:
template_name – The name of the template to render (when used as first
arg). (positional)
block_name (
Optional[str]) – The name of a single template block to render.block_names (
Optional[list[str]]) – A list of template block names to render. Only one of block_name and block_names can be set.
- Raises:
ValueError – If both block_name and block_names are set.
- to_asgi_response(app, request, *, background=None, cookies=None, encoded_headers=None, headers=None, is_head_response=False, media_type=None, status_code=None, type_encoders=None)[source]
Create an ASGIResponse from a Response instance.
- Parameters:
app (
Optional[Litestar]) – TheLitestarapplication instance.background (
Union[BackgroundTask,BackgroundTasks,None]) – Background task(s) to be executed after the response is sent.cookies (
Optional[Iterable[Cookie]]) – A list of cookies to be set on the response.encoded_headers (
Optional[Iterable[Tuple[bytes,bytes]]]) – A list of already encoded headers.headers (
Optional[Dict[str,str]]) – Additional headers to be merged with the response headers. Response headers take precedence.is_head_response (
bool) – Whether the response is a HEAD response.media_type (
Union[MediaType,str,None]) – Media type for the response. Ifmedia_typeis already set on the response, this is ignored.request (
Request) – TheRequestinstance.status_code (
Optional[int]) – Status code for the response. Ifstatus_codeis already set on the response, this istype_encoders – A dictionary of type encoders to use for encoding the response content.
- Return type:
ASGIResponse- Returns:
An ASGIResponse instance.
- context
- template_name
- template_str