30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
from typing import Optional
|
|
from fastapi import Request
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
|
|
class BaseTemplateResponse:
|
|
"""Базовый метод для рендеринга Jinja2 темплейтов.
|
|
|
|
Нужен в первую очередь для того, чтобы подставлять какие-то дефолтные значения (которые есть
|
|
на всех страницах) в переменные темплейтов
|
|
|
|
Attributes:
|
|
templates: Формат темплейтов, используемых в проекте
|
|
"""
|
|
def __init__(self, templates: Jinja2Templates):
|
|
self.templates = templates
|
|
|
|
def render(
|
|
self, request: Request, template_name: str, context: Optional[dict] = None
|
|
):
|
|
base_context = {
|
|
"request": request,
|
|
"title": "Fitness stats",
|
|
"app_name": "Fitness parser"
|
|
}
|
|
|
|
if context:
|
|
base_context.update(context)
|
|
|
|
return self.templates.TemplateResponse(template_name, base_context)
|