I have navigation bar, such as :
<div id="nav">
<ul>
<li
% if request.current_route_path == "somepath":
class="current"
% endif
> <a href='/page1"> 1 </a></li>
<li
% if request.current_route_path == "another_test":
class="current"
%endif
> <a href="/page2"> 2 </a> <li>
</ul>
</div>
I want to add some codes to test what is the current route
so that I can decide which bar to be highlighted (class="current"
).
I know Pyramid has a method current_route_path
to get the path of current URL. However, I think it's better to use route name instead of route path. Does anyone have ideas about this?
What you want is to use the matched_route
.
if request.matched_route.name == 'my_route_name':
I think you are looking for something like this:
% if request.url == request.route_url('my_route_name')
##do stuff
%endif
where request.url is the current url and request.route_url('my_route_name') is the url of a named route
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With