Skip to content

fastapi_tools.templating

Jinja2 template engine configuration.

Functions:

configure_templates

configure_templates(
    templates: Jinja2Templates, config: WebappConfig
) -> None

Inject application-wide globals into the Jinja2 environment.

Parameters:

  • templates (Jinja2Templates) –

    Jinja2Templates instance to configure.

  • config (WebappConfig) –

    Webapp configuration containing app metadata.

Source code in src/fastapi_tools/templating.py
def configure_templates(templates: Jinja2Templates, config: WebappConfig) -> None:
    """Inject application-wide globals into the Jinja2 environment.

    Args:
        templates: Jinja2Templates instance to configure.
        config: Webapp configuration containing app metadata.
    """
    templates.env.globals.update(
        {
            "app_name": config.app_name,
            "app_version": config.app_version,
            "debug": config.debug,
        }
    )

make_templates

make_templates(
    templates_dir: Path | str,
) -> Jinja2Templates

Create a Jinja2Templates instance for the given directory.

Parameters:

  • templates_dir (Path | str) –

    Path to the templates directory.

Returns:

  • Jinja2Templates

    Configured Jinja2Templates instance.

Source code in src/fastapi_tools/templating.py
def make_templates(templates_dir: Path | str) -> Jinja2Templates:
    """Create a Jinja2Templates instance for the given directory.

    Args:
        templates_dir: Path to the templates directory.

    Returns:
        Configured Jinja2Templates instance.
    """
    return Jinja2Templates(directory=str(templates_dir))