jinja

class litestar.contrib.jinja.JinjaTemplateEngine[原始碼]

基底類別:TemplateEngineProtocol[JinjaTemplate, Mapping[str, Any]]

The engine instance.

__init__(directory: Path | list[Path] | None = None, engine_instance: Environment | None = None) None[原始碼]

Jinja-based TemplateEngine.

參數:
  • directory -- Direct path or list of directory paths from which to serve templates.

  • engine_instance -- A jinja Environment instance.

get_template(template_name: str) JinjaTemplate[原始碼]

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

參數:

template_name -- A dotted path

回傳:

JinjaTemplate instance

引發:

TemplateNotFoundException -- if no template is found.

register_template_callable(key: str, template_callable: Callable[[Concatenate[Mapping[str, Any], P]], T]) None[原始碼]

Register a callable on the template engine.

參數:
  • key -- The callable key, i.e. the value to use inside the template to call the callable.

  • template_callable -- A callable to register.

回傳:

None

render_string(template_string: str, context: Mapping[str, Any]) str[原始碼]

Render a template from a string with the given context.

參數:
  • template_string -- The template string to render.

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

回傳:

The rendered template as a string.

classmethod from_environment(jinja_environment: Environment) JinjaTemplateEngine[原始碼]

Create a JinjaTemplateEngine from an existing jinja Environment instance.

參數:

jinja_environment (jinja2.environment.Environment) -- A jinja Environment instance.

回傳:

JinjaTemplateEngine instance