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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码