-2

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));
Keks
  • 11
  • 4
  • 1
    `$this->render(...` will work. What problems did you have with it? – Yoshi Sep 09 '15 at 12:06
  • I got a JsonResponse with error, after inserting this line. Is my routing correct? Bundlename:Entityname:Templatename? My template is placed in Resources/views/Entityname – Keks Sep 09 '15 at 12:10
  • 1
    Please post all relevant code/errors you have. Currently this is just guesswork. `$this->render` will give you a string (probably html), it all depends on how you further use this string. – Yoshi Sep 09 '15 at 12:17
  • I dont even use it. I just put it in a variable. – Keks Sep 09 '15 at 12:21
  • 1
    The error is still missing. – Yoshi Sep 09 '15 at 12:27
  • 1
    I've done this many times as well and never had any problems. I have a feeling, though, that you might have to pass the necessary variables to `MyBundle:ENtity:show.html.twig` when rendering. For example `$this->render('MyBundle:Entity:show.html.twig', array('entity' => $entity))`? Again, having an error and the relevant code would help, or we could keep guessing forever. – tftd Sep 09 '15 at 12:32
  • I dont get one. I debugged it with firebug and it holds on erroer function of my ajax call. – Keks Sep 09 '15 at 12:33
  • @LauraE. try opening the url you request through ajax directly in your browser. – Yoshi Sep 09 '15 at 12:33
  • this may be the problem. ill check it. – Keks Sep 09 '15 at 12:33
  • Could you pls give me a full example with: template, variables, aciion etc.? – Keks Sep 09 '15 at 12:47
  • What i want to do is: render 1 special table of my show.html.twig and send it via jsonresponse, to replace the old one – Keks Sep 09 '15 at 12:50
  • Seems like you absolutely don't know what you try to do, first you save just fetched entity, later you render template without any variables, and even you not pass the template to response... – malcolm Sep 09 '15 at 13:01
  • y i donz pass it yet, because i cant event get it.. this would be my next step.. im new to symfony and if this isnt the best way to get what i want, pls tell me a better one =) – Keks Sep 09 '15 at 13:03
  • It is unclear what you are asking for – Michael Sivolobov Sep 09 '15 at 13:14
  • Ok. I am adding a new element via ajax. Works perfect. Now I want to update the table, that shows all elements. I tried this with $('#id').load... but its very slow. That's why I want to send that table as part of my jsonresponse. – Keks Sep 09 '15 at 13:24

1 Answers1

0

Don't use the json response, that's for sending data like arrays or objects to the view. you want to use $this->render and then as the ajax success function you can have something like this

function(msg){

    $e = $('your_element'); // this is where you want to display the received data
    $e.html(msg);
}

See This for more on ajax, Symfony2 and Json.

Community
  • 1
  • 1
user2268997
  • 1,263
  • 2
  • 14
  • 35
  • Can you please look at my edits above and try to figure out what I did wrong on calling the template? – Keks Sep 09 '15 at 14:37