How do I return a twig rendered template as part of a JSON response?
18,784
Use $this->renderView()
instead.
$this->render()
returns a Response
object, while $this->renderView()
returns a string resulting from rendering a template.
Related videos on Youtube
Author by
ed209
Updated on September 14, 2022Comments
-
ed209 over 1 year
I want to return an HTML snippet as well as other values in a json string, here is what I have:
$html = $this->render('sometemplate.html.twig', array( 'somevar' => $somevar ) ); $response = new Response(json_encode( array("html" => $html, "name" => "Joe Bloggs") )); $response->headers->set('Content-Type', 'application/json'); return $response;
But all I get is
{"html":{"headers":{}}}
. Is there a way to just grab the rendered HTML?