"""Where Steward's templates live, for the guards that read every one, and a way to render a plugin's template in a test.""" import pathlib from jinja2 import ChoiceLoader, FileSystemLoader import steward REPO = pathlib.Path(steward.__file__).parent.parent def template_files() -> list[pathlib.Path]: """Every first-party .html template: core and each plugin's.""" roots = [REPO / "steward" / "templates", *(REPO / "plugins").glob("*/templates")] return sorted(p for root in roots for p in root.rglob("*.html")) async def render_plugin_template(app, plugin: str, name: str, **ctx) -> str: """Render plugins//templates/ through the app's own Jinja environment (its globals, filters and request context), whether or not the plugin is enabled in this app. Call inside app.test_request_context().""" env = app.jinja_env.overlay(loader=ChoiceLoader( [FileSystemLoader(str(REPO / "plugins" / plugin / "templates")), app.jinja_env.loader])) await app.update_template_context(ctx) template = env.get_template(name) return await template.render_async(**ctx) if env.is_async else template.render(**ctx)