模板¶
- class litestar.template.TemplateConfig¶
基类:
Generic[EngineType]Configuration for Templating.
To enable templating, pass an instance of this class to the
Litestarconstructor 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.
- class litestar.template.TemplateEngineProtocol¶
基类:
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.
- 参数:
template_name¶ -- A dotted path
- 返回:
Template instance
- 抛出:
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.