Symfony2 internal route in Twig render function

喜欢而已 提交于 2019-12-01 11:26:18

Better do not use {{ render(controller('AcmeDemoBundle:Page:mainmenu')) }}. It work more fast and comfortable when you use services instead. You can create a service which will show menu on any page where you include them. And in service you can easy get access to current _route for add active class.

But if you really need to use {{ render(controller('AcmeDemoBundle:Page:mainmenu')) }}, so you need pass to them a current request like:

{{ render(controller('AcmeDemoBundle:Page:mainmenu', {'request': app.request})) }}

and then in action pass request to twig:

public function mainmenuAction($request) {

    return $this->render('AcmeDemoBundle:Page:mainmenu.html.twig', array('request' => $request));
}

and in twig use this request:

{% if menu_pages is defined %}
    {% for page in menu_pages %}
        <li class="{% if request.get('_route') == '_page_show' and request.get('id') == page.id %}active{% endif %}"><a href="{{ path('_page_show', {id: page.id}) }}">{{ page.title|e }}</a></li>
    {% endfor %}
{% endif %}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!