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