title: how to get json encode from render function in symfony2
body: <p>I'm trying to work with ajax in symfony but is some hard for me. I have a form in twig where I get a value that I'm using for search a product in my database:</p>
  
  <p>This is the form in twig:</p>
  
  <p><code>&lt;form id="myForm" method="post" action="{{ path('my_app_greeting') }}"&gt;
        &lt;div class="input-group"&gt;
            &lt;input id="name_id" name="name" type="text" class="form-control" placeholder="Buscar repuesto..." required /&gt;
            &lt;span class="input-group-btn"&gt;
                &lt;button id="search-button" class="btn btn-default"             type="button"&gt;Buscar&lt;/button&gt;
            &lt;/span&gt;
        &lt;/div&gt;&lt;!-- /input-group --&gt;&lt;/br&gt;
      &lt;/form&gt;</code></p>
  
  <p>This is my code using jquery in the same twig template for send the data with ajax:</p>
  
  <pre><code>$(document).ready(function(){
      $("#myForm").submit(function(){
      var value = $("#name_id").val();
      $.ajax({
          type: "POST",
          data: { productValue: value },
          url: "/ventas/test/ajax/"
      })
      .done(function(response){
          template = response;
          $('#output').html(template.content); 
      })
      .fail(function(jqXHR, textStatus, errorThrown){
          alert('Error : ' + errorThrown);
      });
      return false;
      });
  });
  </code></pre>
  
  <p>Later I read about how symfony works with ajax and I thought the best solution for my "problem" is create a new twig template called "ajaxContent.html.twig" for example and do this with the controller:</p>
  
  <pre><code>public function testAction()
  {
      $request  = $this-&gt;get('request');
      $name     = $request-&gt;request-&gt;get('productValue');
      $template = $this-&gt;renderView('AcmeDemoTwoBundle:Ventas:ajaxContent.html.twig', array('value' =&gt; $name));
      $json     = json_encode($template);
      $response = new Response($json, 200);
      $response-&gt;headers-&gt;set('Content-Type', 'application/json');
      return new Response($response);
  }
  </code></pre>
  
  <p>Of this way later I want to put all the html content inside my original template but I have a problem with json encode. I don't understand very well how it works and how is the better way for receive the data and show it in the twig template. How can I do this? ( I tried to show the data using <code>$('#output').html(template.content);</code> but doesn't work).</p>
  
  <p>Thanks for your help!</p>