I’ve got layout, something like that:
{# ... #}
{% render 'PamilGooglePlusBundle:Default:sidebar' %}
{# ... #}
{{ globalVariable }}
In PamilGooglePlusBundle:Default:sidebar I run 2 queries using DBAL whose generate me list of users and groups. I have function in sidebarAction() which gave me name of actual resource: group or user name. I want to use it in other part of template.
I got to some thoughts. I have to run this method each query and get its variable every time, how to do it? I mean some kind of controller method which is always done so I can get the variable.
I solved this problem! 😉
Simply, we make Twig extensions, register there init function with some parameters, use it in main template and values are register globals – just like in this code:
Now the
services.yml:And we can use it in template, for example
main.html.twig:In
sidebar.html.twig:Hope it will help someone else 😉