Skip to content

TemplateEngine

Base classes

Name Children Inherits
Protocol
typing
Base class for protocol classes.

⋔ Inheritance diagram

graph TD
  2442760286["engine.TemplateEngine"]
  1073422405["typing.Protocol"]
  3481597255["typing.Generic"]
  2751680898["builtins.object"]
  1073422405 --> 2442760286
  3481597255 --> 1073422405
  2751680898 --> 3481597255

🛈 DocStrings

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
@runtime_checkable
class TemplateEngine(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 `LookupError`s); 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.
    """

    engine_name: ClassVar[str]
    """Name of the backend (`"jinja2"`, `"minijinja"`)."""
    filters: dict[str, Callable[..., Any]]
    """Filters by name (`{{ x | name }}`)."""
    tests: dict[str, Callable[..., bool]]
    """Tests by name (`{% if x is name %}`)."""
    globals: dict[str, Any]
    """Global variables / functions of all templates."""
    loader: jinja2.BaseLoader | None
    """The template loader (`None`: only template strings can be rendered)."""

    def render_string(
        self,
        string: str,
        variables: dict[str, Any] | None = None,
        **kwargs: Any,
    ) -> str:
        """Render a template string with given variables."""
        ...

    def render_template(
        self,
        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)."""
        ...

    def render_condition(
        self,
        string: str,
        variables: dict[str, Any] | None = None,
        **kwargs: Any,
    ) -> bool:
        """Render a template string and return whether the result is truthy."""
        ...

    def render_file(
        self,
        file: str | os.PathLike[str],
        variables: dict[str, Any] | None = None,
        **kwargs: Any,
    ) -> str:
        """Render a file (local path or fsspec URL) as template string."""
        ...

    def get_source(self, template_name: str) -> str:
        """Return the source of a template of the loader."""
        ...

    def set_undefined(self, value: undefined_.UndefinedStr) -> None:
        """Set the behaviour for undefined variables (`strict`, `silent`, ...)."""
        ...

    def load_jinja_file(
        self,
        path: str | os.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."""
        ...

    def add_template(self, file: str | os.PathLike[str]) -> None:
        """Make a file available as template (its path is the template name)."""
        ...

    def add_template_path(self, *path: str | os.PathLike[str]) -> None:
        """Add folders to search templates in."""
        ...

    def with_globals(self, **kwargs: Any) -> contextlib.AbstractContextManager[None]:
        """Set globals temporarily (not thread-safe: changes the shared dict)."""
        ...

    def __contains__(self, template: str | os.PathLike[str]) -> bool:
        """Whether the loader knows given template."""
        ...

engine_name class-attribute

engine_name: str

Name of the backend ("jinja2", "minijinja").

filters instance-attribute

filters: dict[str, Callable[..., Any]]

Filters by name ({{ x | name }}).

globals instance-attribute

globals: dict[str, Any]

Global variables / functions of all templates.

loader instance-attribute

loader: BaseLoader | None

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__

__contains__(template: str | PathLike[str]) -> bool

Whether the loader knows given template.

Source code in src/jinjarope/engine.py
def __contains__(self, template: str | os.PathLike[str]) -> bool:
    """Whether the loader knows given template."""
    ...

add_template

add_template(file: str | PathLike[str]) -> None

Make a file available as template (its path is the template name).

Source code in src/jinjarope/engine.py
def add_template(self, file: str | os.PathLike[str]) -> None:
    """Make a file available as template (its path is the template name)."""
    ...

add_template_path

add_template_path(*path: str | PathLike[str]) -> None

Add folders to search templates in.

Source code in src/jinjarope/engine.py
def add_template_path(self, *path: str | os.PathLike[str]) -> None:
    """Add folders to search templates in."""
    ...

get_source

get_source(template_name: str) -> str

Return the source of a template of the loader.

Source code in src/jinjarope/engine.py
def get_source(self, template_name: str) -> str:
    """Return the source of a template of the loader."""
    ...

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
def load_jinja_file(
    self,
    path: str | os.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."""
    ...

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.

Source code in src/jinjarope/engine.py
def render_condition(
    self,
    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.

Source code in src/jinjarope/engine.py
def render_file(
    self,
    file: str | os.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 a template string with given variables.

Source code in src/jinjarope/engine.py
def render_string(
    self,
    string: str,
    variables: dict[str, Any] | None = None,
    **kwargs: Any,
) -> str:
    """Render a template string with given variables."""
    ...

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
def render_template(
    self,
    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)."""
    ...

set_undefined

set_undefined(value: UndefinedStr) -> None

Set the behaviour for undefined variables (strict, silent, ...).

Source code in src/jinjarope/engine.py
def set_undefined(self, value: undefined_.UndefinedStr) -> None:
    """Set the behaviour for undefined variables (`strict`, `silent`, ...)."""
    ...

with_globals

with_globals(**kwargs: Any) -> AbstractContextManager[None]

Set globals temporarily (not thread-safe: changes the shared dict).

Source code in src/jinjarope/engine.py
def with_globals(self, **kwargs: Any) -> contextlib.AbstractContextManager[None]:
    """Set globals temporarily (not thread-safe: changes the shared dict)."""
    ...

:fa-brands-github: Show source on GitHub