Jinja
Jinja2 (renderer) decorator factory.
Source code in fasthx/jinja.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
make_context: JinjaContextFactory = JinjaContext.unpack_result
class-attribute
instance-attribute
Function that will be used by default to convert a route's return value into
a Jinja rendering context. The default value is JinjaContext.unpack_result
.
templates: Jinja2Templates
instance-attribute
The Jinja2 templates of the application.
__call__(template_name, *, no_data=False, make_context=None)
Deprecated and will be removed in the future. Please use hx()
instead.
Alias for hx()
.
Source code in fasthx/jinja.py
hx(template_name, *, no_data=False, make_context=None)
Decorator for rendering a route's return value to HTML using the Jinja2 template with the given name, if the request was an HTMX one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template_name |
str
|
The name of the Jinja2 template to use. |
required |
no_data |
bool
|
If set, the route will only accept HTMX requests. |
False
|
make_context |
JinjaContextFactory | None
|
Route-specific override for the |
None
|
Returns:
Type | Description |
---|---|
Callable[[MaybeAsyncFunc[P, Any]], Callable[P, Coroutine[None, None, Any | Response]]]
|
The rendered HTML for HTMX requests, otherwise the route's unchanged return value. |
Source code in fasthx/jinja.py
page(template_name, *, make_context=None)
Decorator for rendering a route's return value to HTML using the Jinja2 template with the given name. This decorator triggers HTML rendering regardless of whether the request was HTMX or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template_name |
str
|
The name of the Jinja2 template to use. |
required |
make_context |
JinjaContextFactory | None
|
Route-specific override for the |
None
|
Source code in fasthx/jinja.py
template(template_name, *, no_data=False, make_context=None)
Deprecated and will be removed in the future. Please use hx()
instead.
Alias for hx()
.
Source code in fasthx/jinja.py
JinjaContext
Core JinjaContextFactory
implementations.
Source code in fasthx/jinja.py
unpack_result(*, route_result, route_context)
classmethod
Jinja context factory that tries to reasonably convert non-dict
route results
to valid Jinja contexts (the route_context
argument is ignored).
Supports dict
and Collection
instances, plus anything with __dict__
or __slots__
attributes, for example Pydantic models, dataclasses, or "standard" class instances.
Conversion rules:
dict
: returned as is.Collection
: returned as{"items": route_context}
, available in templates asitems
.- Objects with
__dict__
or__slots__
: known keys are taken from__dict__
or__slots__
and the context will be created as{key: getattr(route_result, key) for key in keys}
, omitting property names starting with an underscore. None
is converted into an empty context.
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in fasthx/jinja.py
unpack_result_with_route_context(*, route_result, route_context)
classmethod
Jinja context factory that tries to reasonably convert non-dict
route results
to valid Jinja contexts, also including every key-value pair from route_context
.
Supports everything that JinjaContext.unpack_result()
does and follows the same
conversion rules.
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in fasthx/jinja.py
JinjaContextFactory
Bases: Protocol
Protocol definition for methods that convert a FastAPI route's result and route context
(i.e. the route's arguments) into a Jinja context (dict
).
Source code in fasthx/typing.py
__call__(*, route_result, route_context)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
route_result |
Any
|
The result of the route. |
required |
route_context |
dict[str, Any]
|
Every keyword argument the route received. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The Jinja context dictionary. |
Raises:
Type | Description |
---|---|
ValueError
|
If converting the arguments to a Jinja context fails. |