How can i pass parameters to a Symfony2 Twig block?

There is an update to the include tag in Symfony 2.2 which might help you with this. Here's an example of the new tag: {{ include('FTWGuildBundle:Help:popover.html.twig', {'content':helpContent,'title':helpTitle}) }}

This may be what you need, since it avoids having to do a sub-request to a controller (render does this) it will be better performing.

In my example, I'm including the HTML for a help popover and providing the title and content.


Now with Symfony v2+ (3, 4 & 5, since Twig v1.28.0), we can use a custom template on the block() function using the with keyword:

{% with {
            'myVar1': myValue1,
            'myVar2': myValue2
        }
%}
        {{ block('toolbar', myTemplate) }}
{% endwith %}

Commit: https://github.com/twigphp/Twig/commit/02b084e2f5c3119604b1c0da388dd2438a012191

Tags:

Php

Twig

Symfony