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:
我是Symfony2的新手,我需要能够在TWIG中测试当前的路线,这样我就可以在一个模板中显示子菜单,这个模板呈现如下:
{% 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 个解决方案
#1
22
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()
.
如果您想在视图中执行此操作,只需将app.request.attributes.get('_route')与所需的路由进行比较。我不明白你为什么要设置path()。
{% if app.request.attributes.get('_route') == 'my_route' %}
{% endif %}
#1
22
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()
.
如果您想在视图中执行此操作,只需将app.request.attributes.get('_route')与所需的路由进行比较。我不明白你为什么要设置path()。
{% if app.request.attributes.get('_route') == 'my_route' %}
{% endif %}