8

我有导航栏,例如:

<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>

我想添加一些代码来测试当前是什么,route以便我可以决定要突出显示哪个栏(class="current")。

我知道 Pyramid 有一种方法current_route_path可以获取当前 URL 的路径。但是,我认为最好使用route name而不是route path。有人对此有想法吗?

4

2 回答 2

34

你想要的是使用matched_route.

if request.matched_route.name == 'my_route_name':
于 2012-11-25T17:37:43.663 回答
1

我想你正在寻找这样的东西:

% if request.url == request.route_url('my_route_name')
##do stuff
%endif

其中 request.url 是当前 url,request.route_url('my_route_name') 是命名路由的 url

于 2012-11-25T17:03:12.633 回答