dongqing220586 2012-05-20 18:54
浏览 543
已采纳

如何在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?

  • 写回答

1条回答 默认 最新

  • dorbmd1177 2012-05-20 19:13
    关注

    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 %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制