Skip to content
Advertisement

How to inject global variables into all templates?

In my twig template there are some variables that are required on each page like userName, userId or userImage.

What is the best way to inject them into the template?

I already read the article How to Inject Variables into all Templates but how could I write a variable (e.g. current user) into the config/packages/twig.php file?

Right now there is code like this in every controller:

JavaScript

(These are only example values as I didn’t integrate the Authentication yet but wanted to create my template)

Is there any better way than injecting every variable in each controller?

Advertisement

Answer

You have several options, depending exactly on what do you need:

###You could inject this as Twig global variables using an Event Listener.

  1. Create a listener class:
JavaScript

I’m injecting:

  • Twig, so you can add the global variables there.
  • Entity Manager, so you can query the DB if you need to grab info from there
  • Security, to grab information from the user.
  1. After this is created, you need to add this configuration lines:
JavaScript

which will enable the event listener to run on each request.

Use twig configuration to inject a service-as-a-global:

With these configuration lines, as described in the documentation, you tell twig to inject a specific service:

JavaScript

Then it’s as simple as creating AppServiceUserManagement:

JavaScript

Then from twig you can do:

JavaScript

Note that every time you called users.OldestUser the method will be executed, which could be resource intensive depending on what you are doing. Caching the result on a local property for that service would probably be a good idea.

Finally, if you are only interested in logged-in user data

As others mentioned, if you are only interested in data belonging to the logged-in user, you can simply access it through the app variable, which is injected on each request.

The representation of the current user or null if there is none. The value stored in this variable can be a UserInterface object, any other object which implements a __toString() method or even a regular string.

For example:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement