After creating a new element using ajax, I get a JsonResponse from my conroller. Now I want to update the elements list. Therefor I want to have the new table as a variable in my JsonResponse.
How do I render a template in my Controller Action?
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBundle:Entity')->find($id);
$em->persist($entity);
$em->flush();
$template = $this->render('MyBundle:Entity:show.html.twig');
return new JsonResponse(array('message' => 'Success!'), 200);
When I add the "template"-line, i get an error on my jsonresponse. The entity is added to the database correctly.
My Template. show.html.twig
<table class="table table-responsive table-hover">
<thead>
<tr>
<th>Titel</th>
<th>Link</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td>{{ entity.titel }}</td>
<td>{{ entity.link }}</td>
</tr>
{% endfor %}
</tbody>
</table>
My Call
$template = $this->render('MyBundle:Entity:show.html.twig', array('entities'=> $entities));