-
Would be great to get an access to a route name variable in a template instead of pass it into a context every single time. What i doHandler ...
@get("/login", name='login')
async def login_page(self, request: Request) -> Template:
return Template(template_name='login.html', context=dict(route_name='login'))
... Template
What would be goodTemplate
Is possible to get an access to? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Heya, what you propose Here is an example that uses HTMX and Jinja as two separate routes. The assert passes, meaning it does have access and is able to get the name. from litestar import get
from litestar.contrib.htmx.request import HTMXRequest
from litestar.contrib.htmx.response import HTMXTemplate
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.response import Template
from litestar.template.config import TemplateConfig
from litestar.testing import create_test_client
@get("hello_htmx", name="hello_htmx_route")
async def hello_htmx() -> HTMXTemplate:
return HTMXTemplate(
template_str="url is {{url_for(request.route_handler.name)}}", push_url="/form"
)
@get("hello", name="hello_route")
async def hello() -> Template:
return Template(template_str="url is {{url_for(request.route_handler.name)}}")
with create_test_client(
[hello_htmx],
request_class=HTMXRequest,
template_config=TemplateConfig(directory=".", engine=JinjaTemplateEngine),
) as client:
response = client.get("hello_htmx")
assert response.text == "url is /hello_htmx"
with create_test_client(
[hello],
template_config=TemplateConfig(directory=".", engine=JinjaTemplateEngine),
) as client:
response = client.get("hello")
assert response.text == "url is /hello" Now, if your question is to have a default name to the router itself, then it is an ongoing issue discussed over at #2892 |
Beta Was this translation helpful? Give feedback.
-
Oh, i see. |
Beta Was this translation helpful? Give feedback.
Heya, what you propose
url_for(request.route_handler.name)
already works inside a template.request
is passed by default into the template context.Here is an example that uses HTMX and Jinja as two separate routes. The assert passes, meaning it does have access and is able to get the name.