template#
- class litestar.template.TemplateEngineProtocol#
Bases:
Protocol
[TemplateType_co
,ContextType_co
]Protocol for template engines.
- __init__(directory: Path | list[Path] | None, engine_instance: Any | None) None #
Initialize the template engine with a directory.
- get_template(template_name: str) TemplateType_co #
Retrieve a template by matching its name (dotted path) with files in the directory or directories provided.
- Parameters:
template_name¶ – A dotted path
- Returns:
Template instance
- Raises:
TemplateNotFoundException – if no template is found.
- render_string(template_string: str, context: Mapping[str, Any]) str #
Render a template from a string with the given context.
- register_template_callable(key: str, template_callable: Callable[[Concatenate[ContextType_co, P]], R]) None #
Register a callable on the template engine.
- class litestar.template.TemplateProtocol#
Bases:
Protocol
Protocol Defining a
Template
.Template is a class that has a render method which renders the template into a string.
- __init__(*args, **kwargs)#
- class litestar.template.TemplateConfig#
Bases:
Generic
[EngineType
]Configuration for Templating.
To enable templating, pass an instance of this class to the
Litestar
constructor using the ‘template_config’ key.- engine: type[EngineType] | EngineType | None = None#
A template engine adhering to the
TemplateEngineProtocol
.
- directory: PathType | list[PathType] | None = None#
A directory or list of directories from which to serve templates.
- engine_callback: Callable[[EngineType], None] | None = None#
A callback function that allows modifying the instantiated templating protocol.
- __init__(engine: type[EngineType] | EngineType | None = None, directory: PathType | list[PathType] | None = None, engine_callback: Callable[[EngineType], None] | None = None, instance: EngineType | None = None) None #
- to_engine() EngineType #
Instantiate the template engine.
- property engine_instance: EngineType#
Return the template engine instance.