minijinja#

MiniJinja template engine integration for Litestar.

class litestar.plugins.minijinja.MiniJinjaTemplate#

Bases: TemplateProtocol

Initialize a template.

Parameters:

template – Base MiniJinjaTemplate used by the underlying minijinja engine

__init__(engine: Any, template_name: str) None#
render(*args: Any, **kwargs: Any) str#

Render a template.

Parameters:
  • args – Positional arguments passed to the engines render function

  • kwargs – Keyword arguments passed to the engines render function

Returns:

Rendered template as a string

class litestar.plugins.minijinja.MiniJinjaTemplateEngine#

Bases: TemplateEngineProtocol[MiniJinjaTemplate, StateProtocol]

The engine instance.

__init__(directory: Path | list[Path] | None = None, engine_instance: Any | None = None) None#

Minijinja based TemplateEngine.

Parameters:
  • directory – Direct path or list of directory paths from which to serve templates.

  • engine_instance – A Minijinja Environment instance.

get_template(template_name: str) MiniJinjaTemplate#

Retrieve a template by matching its name (dotted path) with files in the directory or directories provided.

Parameters:

template_name – A dotted path

Returns:

MiniJinjaTemplate instance

Raises:

TemplateNotFoundException – if no template is found.

register_template_callable(key: str, template_callable: Callable[[Concatenate[StateProtocol, P]], T]) None#

Register a callable on the template engine.

Parameters:
  • key – The callable key, i.e. the value to use inside the template to call the callable.

  • template_callable – A callable to register.

Returns:

None

render_string(template_string: str, context: Mapping[str, Any]) str#

Render a template from a string with the given context.

Parameters:
  • template_string – The template string to render.

  • context – A dictionary of variables to pass to the template.

Returns:

The rendered template as a string.

classmethod from_environment(minijinja_environment: Any) MiniJinjaTemplateEngine#

Create a MiniJinjaTemplateEngine from an existing minijinja Environment instance.

Parameters:

minijinja_environment (Environment) – A minijinja Environment instance.

Returns:

MiniJinjaTemplateEngine instance

class litestar.plugins.minijinja.StateProtocol#

Bases: Protocol

__init__(*args, **kwargs)#