如何在TWIG中获取当前的URL或路由?

如何在TWIG中获取当前的URL或路由?

问题描述:

I'm very new to Symfony2 and I need to be able to test the current route in TWIG so I can display sub-menus in a template that's rendered like:

{% render "CPAdminBundle:Messages:sidebarMenu" %}
{% render "CPAdminBundle:Readings:sidebarMenu" %}

Within the sidebar templates I tried using the following but it throws an error:

path(app.request.attributes.get('_route')) 

What's the correct way of doing what I'm trying to accomplish?

我是Symfony2的新手,我需要能够在TWIG中测试当前路线,这样才能显示 模板中的子菜单呈现为: p>

  {%render“CPAdminBundle:Messages:sidebarMenu”%} 
 {%render“CPAdminBundle:读数:sidebarMenu”%}  
  code>  pre> 
 
 

在侧边栏模板中,我尝试使用以下内容但它会引发错误: p>

  path(app。  request.attributes.get('_ route'))
  code>  pre> 
 
 

我正在努力完成的工作是什么? p> DIV>

The check you want to do doesn't belong to a view. Views should only take care of displaying, not doing any kind of logic.

Do the check in your controller and store it in a variable, pass this variable to your views, and the check the value of this variable in there.
If you want to do this on every action, give the kernel.controller event a look.

If you want to do it in the view anyway, simply compare app.request.attributes.get('_route') to the route you want. I don't understand why you put in path().

{% if app.request.attributes.get('_route') == 'my_route' %}
{% endif %}