engine
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| TemplateEngine jinjarope.engine The engine surface: render strings / templates, register filters / tests / globals. |
||
| TemplateError jinjarope.engine Base class of the errors raised by template engines (all backends). |
||
| TemplateNotFoundError jinjarope.engine A template name could not be resolved by the loader (also a `LookupError`). |
||
| TemplateRenderError jinjarope.engine Rendering a template string failed (also a `RuntimeError`). |
||
| TemplateSyntaxError jinjarope.engine A template could not be parsed (also a `SyntaxError`). |
🛈 DocStrings¶
Template engine protocol: what users of jinjarope need from a template engine.
Two backends implement it:
jinjarope.Environment: Jinja2 (the default, all features)jinjarope.MiniJinjaEnvironment: minijinja (Rust, optional extraminijinja; differences:docs/engines.md)
Both take the same filters / tests / globals (plain Python callables, also the ones
using Jinja2's pass_context / pass_environment markers), the same loaders (the
jinjarope / Jinja2 loader classes) and raise the same errors (below), so code written
against TemplateEngine works with either of them. Pick a backend by name with
get_engine_class().
TemplateEngine
¶
Bases: Protocol
The engine surface: render strings / templates, register filters / tests / globals.
Filters, tests and globals are plain dicts: add or replace entries at any time,
the next render sees them. Templates are found by loader (a Jinja2 /
jinjarope loader; other backends adapt it).
Errors: render_string raises TemplateSyntaxError / TemplateRenderError
(original exception as __cause__). Template lookups by name raise
TemplateNotFoundError (minijinja) or jinja2.TemplateNotFound (Jinja2, both
are LookupErrors); other errors of render_template are the backend's own
(Jinja2: jinja2.TemplateError subclasses, minijinja: minijinja.TemplateError)
or the exception raised by a filter / global.
Source code in src/jinjarope/engine.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 | |
filters
instance-attribute
¶
filters: dict[str, Callable[..., Any]]
Filters by name ({{ x | name }}).
loader
instance-attribute
¶
The template loader (None: only template strings can be rendered).
tests
instance-attribute
¶
tests: dict[str, Callable[..., bool]]
Tests by name ({% if x is name %}).
__contains__
¶
add_template
¶
add_template_path
¶
get_source
¶
load_jinja_file
¶
load_jinja_file(
path: str | PathLike[str],
scope_prefix: str = "",
load_filters: bool = True,
load_tests: bool = True,
load_functions: bool = True,
load_config: bool = True,
load_loader: bool = True,
) -> None
Add the filters / tests / functions (and config, loader) of a JinjaFile.
Source code in src/jinjarope/engine.py
render_condition
¶
render_condition(
string: str, variables: dict[str, Any] | None = None, **kwargs: Any
) -> bool
Render a template string and return whether the result is truthy.
render_file
¶
render_file(
file: str | PathLike[str], variables: dict[str, Any] | None = None, **kwargs: Any
) -> str
Render a file (local path or fsspec URL) as template string.
render_string
¶
render_string(string: str, variables: dict[str, Any] | None = None, **kwargs: Any) -> str
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 only one block of it).
Source code in src/jinjarope/engine.py
set_undefined
¶
with_globals
¶
TemplateError
¶
TemplateNotFoundError
¶
Bases: TemplateError, LookupError
A template name could not be resolved by the loader (also a LookupError).
Source code in src/jinjarope/engine.py
TemplateRenderError
¶
Bases: TemplateError, RuntimeError
Rendering a template string failed (also a RuntimeError).
The original exception (an undefined variable, an exception raised by a filter, ...)
is the __cause__.
TemplateSyntaxError
¶
Bases: TemplateError, SyntaxError
A template could not be parsed (also a SyntaxError).
create_environment
¶
create_environment(name: EngineName | str | None = None, **kwargs: Any) -> TemplateEngine
Create an environment of given backend (keyword arguments: see Environment).
get_engine_class
¶
get_engine_class(name: EngineName | str | None = None) -> type[TemplateEngine]
Return the environment class of given backend (default: Jinja2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
EngineName | str | None
|
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unknown backend name |
ImportError
|
The backend needs a package which is not installed
(minijinja: |