title: How to inject global variables into all templates?
body: <p>In my twig template there are some variables that are required on each page like <code>userName</code>, <code>userId</code> or <code>userImage</code>.</p>
  
  <p>What is the best way to inject them into the template?</p>
  
  <p>I already read the article <a href="https://symfony.com/doc/current/templating/global_variables.html" rel="nofollow noreferrer">How to Inject Variables into all Templates</a> but how could I write a variable (e.g. current user) into the <code>config/packages/twig.php</code> file?</p>
  
  <p>Right now there is code like this in every controller:</p>
  
  <pre class="lang-php prettyprint-override"><code>return $this-&gt;render('admin/users/index.html.twig', [
     "allUsers" =&gt; $allUsers,
     "pageTitle" =&gt; "Benutzerübersicht",
     "userName" =&gt; "Max Mustermann",
     "userId" =&gt; 5,
     "userImage" =&gt; "http://i.pravatar.cc/150?img=6"
  ]);
  </code></pre>
  
  <p>(These are only example values as I didn't integrate the Authentication yet but wanted to create my template)</p>
  
  <p>Is there any better way than injecting every variable in each controller?</p>