Martin Martin - 5 months ago 110
Twig Question

rendering controller action in symfony twig

Updating deprecated code and i have following problem with render tag

> {% render url('_internal_main_navigation', {}) %}

transaltes to

{{ render(controller('MyBundle::menu', {})) }}

Twig does this thing where it appends "Controller" and "Action" to the specified route so That should call
but apparently the Controller class is supposed to have some sort of prefix like "MyController" so the twig route can be specified like

otherwise i get

Class "MyBundle" noes not exist

Can i make it work without prefixing the controller?

Answer Source

I'd recommend to use render_esi() which supports routes via url() by name aswell and you're set for ESI in the future.

When using the default render() function (or setting the renderer to inline), Symfony merges the included page content into the main one before sending the response to the client. But if you use the esi renderer (i.e. call render_esi()) and if Symfony detects that it's talking to a gateway cache that supports ESI, it generates an ESI include tag. But if there is no gateway cache or if it does not support ESI, Symfony will just merge the included page content within the main one as it would have done if you had used render().

See Using ESI in Symfony

The following syntax will work in your case:

{{ render_esi(url('_internal_main_navigation', {})) }}

If you'd like to render by controller-name, you could turn your controller into a service and refer to it by the name of the service (i.e. mybundle_controller):

{{ render_esi(controller('mybundle_controller:menuAction', {})) }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download