Skip to content

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 extra minijinja; 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().

          EngineName

          EngineName = Literal['jinja2', 'minijinja']
          

          Names of the template engine backends.

          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
          @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)."""
              ...
          

          TemplateError

          Bases: Exception

          Base class of the errors raised by template engines (all backends).

          Source code in src/jinjarope/engine.py
          class TemplateError(Exception):
              """Base class of the errors raised by template engines (all backends)."""
          

          TemplateNotFoundError

          Bases: TemplateError, LookupError

          A template name could not be resolved by the loader (also a LookupError).

          Source code in src/jinjarope/engine.py
          class TemplateNotFoundError(TemplateError, LookupError):
              """A template name could not be resolved by the loader (also a `LookupError`)."""
          
              def __init__(self, name: str, message: str | None = None) -> None:
                  self.name = name
                  super().__init__(message or f"Template {name!r} not found")
          

          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__.

          Source code in src/jinjarope/engine.py
          class TemplateRenderError(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).

          Source code in src/jinjarope/engine.py
          class TemplateSyntaxError(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).

          Source code in src/jinjarope/engine.py
          def create_environment(name: EngineName | str | None = None, **kwargs: Any) -> TemplateEngine:
              """Create an environment of given backend (keyword arguments: see `Environment`)."""
              return get_engine_class(name)(**kwargs)
          

          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

          "jinja2" or "minijinja" (None: the default, Jinja2)

          None

          Raises:

          Type Description
          ValueError

          Unknown backend name

          ImportError

          The backend needs a package which is not installed (minijinja: pip install jinjarope[minijinja])

          Source code in src/jinjarope/engine.py
          def get_engine_class(name: EngineName | str | None = None) -> type[TemplateEngine]:
              """Return the environment class of given backend (default: Jinja2).
          
              Args:
                  name: `"jinja2"` or `"minijinja"` (`None`: the default, Jinja2)
          
              Raises:
                  ValueError: Unknown backend name
                  ImportError: The backend needs a package which is not installed
                      (minijinja: `pip install jinjarope[minijinja]`)
              """
              match name or DEFAULT_ENGINE:
                  case "jinja2":
                      from jinjarope.environment import Environment
          
                      return Environment
                  case "minijinja":
                      from jinjarope.minijinjaenv import MiniJinjaEnvironment
          
                      return MiniJinjaEnvironment
                  case _:
                      msg = f"Unknown template engine {name!r} (one of {', '.join(ENGINES)})"
                      raise ValueError(msg)