File size: 1,144 Bytes
7f8e190 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | """Jinja2 environment and shared view context."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from jinja2 import Environment, FileSystemLoader, select_autoescape
from . import content
TEMPLATE_DIR = Path(__file__).resolve().parents[1] / "templates"
environment = Environment(
loader=FileSystemLoader(str(TEMPLATE_DIR)),
autoescape=select_autoescape(("html", "xml")),
trim_blocks=True,
lstrip_blocks=True,
)
environment.globals.update(
categories=content.CATEGORIES,
sort_options=content.SORT_OPTIONS,
status_options=content.STATUS_OPTIONS,
difficulty_options=content.DIFFICULTY_OPTIONS,
language_labels=content.LANGUAGE_LABELS,
all_topics=content.ALL_TOPICS,
all_languages=content.ALL_LANGUAGES,
)
environment.filters["language_label"] = content.language_label
environment.filters["difficulty_label"] = content.difficulty_label
environment.filters["category_label"] = content.category_label
environment.filters["sort_label"] = content.sort_label
def render(name: str, **context: Any) -> str:
return environment.get_template(name).render(**context)
|