Symfony2 - get main request's current route in twig partial/subrequest

前端 未结 3 752
名媛妹妹
名媛妹妹 2020-12-31 18:46

In Twig partial rendered by separate controller, I want to check if current main route equals to compared route, so I can mark list item as active.

How can I do tha

3条回答
  •  無奈伤痛
    2020-12-31 19:10

    I think that the best solution in your case is past the current main route in the render:

    {{ render(controller('FooBundle:Bar:bar', {'current_route' : app.request.uri})) }}
    

    Next, return it in the response:

    public function barAction(Request $request) {
        ...
        return $this->render('Template', array('current_route' => $request->query->get('current_route'));
    }
    

    And in your template compares with the received value.

    Otherwise, maybe is better to use a include instead a render, if you don't need extra logic for the partial.

提交回复
热议问题