title: Symfony2, Twig JSON templates vs json_encode: what's right for this situation
body: <p>Quick question on best practice.</p>
  
  <p>I have an AJAX application where there are 4 routes that all return JSON to be used by jQuery.</p>
  
  <p>I'm currently using something like this at the end of the Controller</p>
  
  <pre><code>return new Response(json_encode($some_array));
  </code></pre>
  
  <p>In one place I use JMSSerializer because the object I'm trying to pass out to Javascript has protected attributes. That never seemed right and I wondered at one point if I should 'teach' the object to know how to encode all its attributes to JSON and send them out as the return value of one of its methods.</p>
  
  <p>Anyway I had a brainwave this morning that what I <strong>should</strong> be doing is to make a Twig JSON template that contains the exact format I want it to output, then fill the variables from the PHP. A couple of loops would also be necessary to output a collection of objects within an object.</p>
  
  <p>That way there's a clear promise/contract from the PHP of what exactly it will return to the Javascript, that can't be messed up if I happen to e.g. change the object. I suspect this would be easier for the guy who's doing the javascript to understand since the template would be easier to read than the PHP.</p>
  
  <p>Am I on the right track: should I be using <strong>JSON Twig templates</strong> rather than json_encode in general?</p>
  
  <p><strong>EDIT</strong> (meanwhile back at the ranch)</p>
  
  <p>Since posting this question my research says:</p>
  
  <p>Twig JSON templates vs json_encode</p>
  
  <ul>
  <li>Would eliminate need for JMSSerializer and not illegally get
  protected object attributes. </li>
  <li>More readable/predictable, particularly for a Javascript guy on the other side.</li>
  <li>Twig will check that variables exist Would immediately flag if the JSON isn't able to
  match the expected format because e.g. object changed. So makes it
  harder to break the friendship between Javascript and PHP.</li>
  <li>Unit test would also do this (and could validate values) but can add that later.</li>
  <li>Separates internal object from external interface meaning as a pattern, we avoid any unwanted specifics about how json_encode or JMSerializer want to form the JSON. Puts me as a human in complete control of what JSON is output.</li>
  <li>Will be slower than json_encode()</li>
  <li>"I would avoid using a template to render the data as the responsibility for escaping data etc is then in the template" says <a href="https://stackoverflow.com/questions/9146460/symfony2-echoing-json-from-a-controller-for-use-in-an-extjs-4-grid">Symfony2: Echoing JSON From a Controller for Use in an ExtJS 4 Grid</a></li>
  <li>Means doing {{ variable_name | json_encode | raw }} a lot: see <a href="http://twig.sensiolabs.org/doc/filters/json_encode.html" rel="nofollow noreferrer">http://twig.sensiolabs.org/doc/filters/json_encode.html</a>  (Raw means turn off auto escaping)</li>
  <li>"It's more efficient to just return the JSON data as the response from the controller anyway as Symfony doesn't need to do the templating stage" says <a href="https://stackoverflow.com/questions/8557412/content-length-0-in-a-json-response-symfony2">Content length: 0 in a json response Symfony2</a></li>
  </ul>