From 7961fa932a92b3743411a3b5307e5ee6636d6904 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Mon, 27 Sep 2021 15:01:45 -0700 Subject: [PATCH] feat(FastAPI): add templates.render_raw_template This function is now used as `render_template`'s underlying implementation of rendering a template, and uses that render in its HTMLResponse path. This separation allows users to directly render a template without producing a Response object. Signed-off-by: Kevin Morris --- aurweb/templates.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/aurweb/templates.py b/aurweb/templates.py index 09be049c..ef020bdf 100644 --- a/aurweb/templates.py +++ b/aurweb/templates.py @@ -102,12 +102,8 @@ async def make_variable_context(request: Request, title: str, next: str = None): return context -def render_template(request: Request, - path: str, - context: dict, - status_code: HTTPStatus = HTTPStatus.OK): +def render_raw_template(request: Request, path: str, context: dict): """ Render a Jinja2 multi-lingual template with some context. """ - # Create a deep copy of our jinja2 _environment. The _environment in # total by itself is 48 bytes large (according to sys.getsizeof). # This is done so we can install gettext translations on the template @@ -119,8 +115,15 @@ def render_template(request: Request, templates.install_gettext_translations(translator) template = templates.get_template(path) - rendered = template.render(context) + return template.render(context) + +def render_template(request: Request, + path: str, + context: dict, + status_code: HTTPStatus = HTTPStatus.OK): + """ Render a template as an HTMLResponse. """ + rendered = render_raw_template(request, path, context) response = HTMLResponse(rendered, status_code=status_code) secure_cookies = aurweb.config.getboolean("options", "disable_http_login") response.set_cookie("AURLANG", context.get("language"),