lnbits-legend/lnbits/jinja2_templating.py

29 lines
1.0 KiB
Python
Raw Normal View History

import typing
from jinja2 import BaseLoader, Environment, pass_context
from starlette.datastructures import QueryParams
2021-09-16 19:42:05 +02:00
from starlette.requests import Request
from starlette.templating import Jinja2Templates as SuperJinja2Templates
class Jinja2Templates(SuperJinja2Templates):
def __init__(self, loader: BaseLoader) -> None:
super().__init__("")
self.env = self.get_environment(loader)
def get_environment(self, loader: BaseLoader) -> Environment:
@pass_context
def url_for(context: dict, name: str, **path_params: typing.Any) -> str:
2021-11-27 13:01:12 +01:00
request: Request = context["request"]
2021-09-16 19:42:05 +02:00
return request.app.url_path_for(name, **path_params)
def url_params_update(init: QueryParams, **new: typing.Any) -> QueryParams:
values = dict(init)
values.update(new)
return QueryParams(**values)
env = Environment(loader=loader, autoescape=True)
env.globals["url_for"] = url_for
env.globals["url_params_update"] = url_params_update
return env