mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
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 <kevr@0cost.org>
This commit is contained in:
parent
5e95cfbc8a
commit
7961fa932a
1 changed files with 9 additions and 6 deletions
|
@ -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"),
|
||||
|
|
Loading…
Add table
Reference in a new issue