"""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 from quart import render_template 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/ with quart.render_template, whether or not the plugin is enabled in this app: the plugin's template folder goes ahead of the app's loader for the one render. Call inside app.test_request_context().""" loader = app.jinja_env.loader app.jinja_env.loader = ChoiceLoader( [FileSystemLoader(str(REPO / "plugins" / plugin / "templates")), loader]) try: return await render_template(name, **ctx) finally: app.jinja_env.loader = loader