title: Symfony2 set default view variables
body: <p>I want to set some variables for all views, is this possible?</p>
  
  <p>I guess I need to do some kind of bootstrap method.</p>
  
  <p>But what I want to achieve is, when I return an array from a function, so that it loads the template. Using a default set up of Symfony2.</p>
  
  <p>Then I would like to append some variables that I will access by Javascript.</p>
  
  <p>For example I always want there to be a user object, so I want it to appear in the layout like </p>
  
  <pre><code>var user = {{user | json_encode()}}
  </code></pre>
  
  <p>And this would then be accessible in javascript. However I do not want to set this variable in each controller I create, I want to add that to all controllers. I would appreciate some help here. I have found the bundle php file, but I don't know if that one will help me.</p>