| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
from typing import Literal |
| 5 |
|
| 6 |
from litestar import Litestar, get |
| 7 |
from litestar.contrib.mako import MakoTemplateEngine |
| 8 |
from litestar.params import FromPath, FromQuery |
| 9 |
from litestar.response import Template |
| 10 |
from litestar.template.config import TemplateConfig |
| 11 |
|
| 12 |
|
| 13 |
@get(path="/{template_type: str}", sync_to_thread=False) |
| 14 |
def index(name: FromQuery[str], template_type: FromPath[Literal["file", "string"]]) -> Template: |
| 15 |
if template_type == "file": |
| 16 |
return Template(template_name="hello.html.mako", context={"name": name}) |
| 17 |
return Template(template_str="Hello <strong>Mako</strong> using strings", context={"name": name}) |
| 18 |
|
| 19 |
|
| 20 |
app = Litestar( |
| 21 |
route_handlers=[index], |
| 22 |
template_config=TemplateConfig( |
| 23 |
directory=Path(__file__).parent / "templates", |
| 24 |
engine=MakoTemplateEngine, |
| 25 |
), |
| 26 |
) |