minijinjaenv
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| EvalContext jinjarope.minijinjaenv What a Jinja2 `pass_eval_context` callable gets under minijinja. |
||
| MiniJinjaEnvironment jinjarope.minijinjaenv A jinjarope environment rendering with minijinja. |
||
| StateContext jinjarope.minijinjaenv What a Jinja2 `pass_context` callable gets under minijinja. |
🛈 DocStrings¶
minijinja backend of the template engine protocol (jinjarope.engine).
MiniJinjaEnvironment renders with minijinja
(Rust) and offers the same API as jinjarope.Environment where jinjarope.engine
needs it: same filters / tests / globals (also Jinja2 pass_context & co. callables),
same loaders, same errors. Differences: docs/engines.md.
Needs the minijinja extra (pip install jinjarope[minijinja]).
UNDEFINED_BEHAVIOR
module-attribute
¶
UNDEFINED_BEHAVIOR: dict[str, MiniJinjaUndefined] = {
"strict": "strict",
"silent": "lenient",
"lax": "chainable",
}
jinjarope's undefined names -> minijinja's undefined behaviour (keep has none).
EvalContext
¶
What a Jinja2 pass_eval_context callable gets under minijinja.
Source code in src/jinjarope/minijinjaenv.py
MiniJinjaEnvironment
¶
Bases: EnvironmentBase
A jinjarope environment rendering with minijinja.
Takes the keyword arguments of jinjarope.Environment; Jinja2-only options
(cache_size, optimized, bytecode_cache) are ignored, unsupported ones
(enable_async, extensions other than loop controls, a newline_sequence other
than a line feed, the keep undefined) raise ValueError.
Source code in src/jinjarope/minijinjaenv.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | |
loader
property
writable
¶
The template loader (a Jinja2 / jinjarope loader).
undefined_behavior
property
¶
The undefined behaviour of minijinja (strict, lenient, chainable).
__contains__
¶
Check whether the loader knows given template.
__init__
¶
__init__(
*,
undefined: UndefinedStr | type[Undefined] = "strict",
trim_blocks: bool = True,
cache_size: int = -1,
auto_reload: bool = False,
loader: (
BaseLoader | list[BaseLoader] | dict[str, Any] | list[dict[str, Any]] | None
) = None,
block_start_string: str = "{%",
block_end_string: str = "%}",
variable_start_string: str = "{{",
variable_end_string: str = "}}",
comment_start_string: str = "{#",
comment_end_string: str = "#}",
line_statement_prefix: str | None = None,
line_comment_prefix: str | None = None,
lstrip_blocks: bool = False,
newline_sequence: Literal["\n", "\r\n", "\r"] = "\n",
keep_trailing_newline: bool = False,
extensions: Sequence[str | type[Extension]] = (),
optimized: bool = True,
finalize: Callable[..., Any] | None = None,
autoescape: bool | Callable[[str | None], bool] = False,
bytecode_cache: BytecodeCache | None = None,
enable_async: bool = False
) -> None
Create the environment (arguments: see jinjarope.Environment).
Source code in src/jinjarope/minijinjaenv.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
add_extension
¶
add_extension(extension: str | type[Extension]) -> None
Accept the Jinja2 extensions minijinja has built in (loop controls).
Raises:
| Type | Description |
|---|---|
ValueError
|
Any other extension (minijinja has no extension API) |
Source code in src/jinjarope/minijinjaenv.py
get_config
¶
get_config() -> EnvConfig
All environment settings (not included: undefined and loaders).
Source code in src/jinjarope/minijinjaenv.py
get_source
¶
Return the source of a template of the loader.
Raises:
| Type | Description |
|---|---|
TemplateNotFoundError
|
The loader doesn't know the template |
Source code in src/jinjarope/minijinjaenv.py
render_string
¶
render_string(string: str, variables: dict[str, Any] | None = None, **kwargs: Any) -> str
Render a template string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
String to render |
required |
variables
|
dict[str, Any] | None
|
Extra variables for the rendering |
None
|
kwargs
|
Any
|
Further extra variables for rendering |
{}
|
Raises:
| Type | Description |
|---|---|
TemplateSyntaxError
|
The string is no valid template |
TemplateRenderError
|
Rendering failed (the original error is the |
Source code in src/jinjarope/minijinjaenv.py
render_template
¶
render_template(
template_name: str,
variables: dict[str, Any] | None = None,
block_name: str | None = None,
parent_template: str | None = None,
**kwargs: Any
) -> str
Render a template of the loader (or a block of it).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_name
|
str
|
Template name |
required |
variables
|
dict[str, Any] | None
|
Extra variables for rendering |
None
|
block_name
|
str | None
|
Render only this block of the template (its own source: blocks inherited from a parent template can't be rendered alone) |
None
|
parent_template
|
str | None
|
Ignored (names are not relative to the including template, like in Jinja2 by default) |
None
|
kwargs
|
Any
|
Further extra variables for rendering |
{}
|
Raises:
| Type | Description |
|---|---|
TemplateNotFoundError
|
The loader doesn't know the template |
BlockNotFoundError
|
The template has no block with given name |
Source code in src/jinjarope/minijinjaenv.py
set_undefined
¶
set_undefined(value: UndefinedStr | type[Undefined]) -> None
Set the undefined behaviour for the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
UndefinedStr | type[Undefined]
|
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
No minijinja equivalent ( |
Source code in src/jinjarope/minijinjaenv.py
StateContext
¶
What a Jinja2 pass_context callable gets under minijinja.
A subset of jinja2.runtime.Context: name, environment, get(), [], in.
Lookups see template variables, render variables and globals. minijinja's lookup
returns None for undefined names, so a variable set to None counts as missing.
Source code in src/jinjarope/minijinjaenv.py
extract_block
¶
extract_block(
source: str,
block_name: str,
block_start: str = "{%",
block_end: str = "%}",
comment_start: str = "{#",
comment_end: str = "#}",
) -> str | None
Return the source inside {% block <block_name> %} ... {% endblock %}.
Nested blocks count (the matching endblock is found), tags inside comments and
raw blocks don't. Whitespace control of the block's own tags (-%} / {%-)
gets applied to the returned source. None if there is no such block.
minijinja's Python API can't render a single block, so render_template renders
this source instead: definitions outside the block (set, import, macros)
are not part of it.