Different template directories in different apps? #4516
Replies: 1 comment 1 reply
-
|
You can only have one template config, but everything else is basically up to you, as Litestar's template integration is simply a convenience wrapper around the underlying template engine / library, in this case, jinja. If you're coming from Django, one thing to keep in mind is that Litestar is fundamentally more decoupled and isolated, putting an emphasis on keeping context boundaries clear. In Django, there's a lot of global state flying around, and apps magically "know" the context they're being invoked from. In Litestar, everything is a bit more explicit. If you really want to, you could add this functionality in some way, for example by creating a custom class RelativeTemplate(Template):
def to_asgi_response(
self,
request: Request,
*,
background: BackgroundTask | BackgroundTasks | None = None,
cookies: Iterable[Cookie] | None = None,
headers: dict[str, str] | None = None,
is_head_response: bool = False,
media_type: MediaType | str | None = None,
status_code: int | None = None,
type_encoders: TypeEncodersMap | None = None,
) -> ASGIResponse:
template_prefix = request.route_handler.opts.get("template_prefix", "")
self.template_path = template_prefix + self.template_path
return super().to_asgi_response(
request=request,
background=background,
cookies=cookies,
headers=headers
is_head_response=is_head_response,
media_type=media_type,
status_code=status_code,
type_encoders=type_encoders,
)and then in your "apps" # suppose this lives in src/some_app, and templates are located in src/some_app/templates
@get("/some-path")
async def some_handler() -> RelativeTemplate:
# this would resolve the template path to src/some_app/templates/some_template.html.jinja2
return RelativeTemplate("some_template.html.jinja2")
router = Router("/some_app_name", [some_handler], opt={"src/some_app/templates/": "some_app_name"}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I'm trying to write a website with several different modules or, if coming from Django, "apps". In each app I want to encapsulate everything that belongs to it: models, controllers, a router, and templates.
Is there a way to define a Template path in each app, so that I don't have to give the full app-path every time? Right now it seems that I can only define a directory once and have to use that setting site-wide?
Beta Was this translation helpful? Give feedback.
All reactions