Reputation:
This is what I am trying to do
public function editAction(Request $request, Uni $uni){
$template = $this->forward('AppBundle:Uni:edit.html.twig')->getContent();
$json = json_encode($template);
$response = new Response($json, 200);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
Basically, I am trying to get the content of edit.html.twig file. Unfortunately, I am not getting the expected content from edit.html.twig file. How can I get this content?
Location of edit.html.twig file is in testproject/app/Resources/uni/edit.html.twig
Upvotes: 2
Views: 2009
Reputation: 36989
You can also use the render
helper directly:
use Symfony\Component\HttpFoundation\JsonResponse;
public function editAction(Request $request, Uni $uni)
{
return $this->render('AppBundle:Uni:edit.html.twig', [
'uni' => $uni,
], new JsonResponse());
}
Upvotes: 0
Reputation: 2488
The simplest way to get the contents of a twig template from inside a controller would be using renderView
helper method.
Another improvement you can make to your code is to use symfony's JsonResponse
class to return your JSON. Example:
use Symfony\Component\HttpFoundation\JsonResponse;
public function editAction(Request $request, Uni $uni)
{
$template = $this->renderView('AppBundle:Uni:edit.html.twig', [
'uni' => $uni,
]);
return new JsonResponse($template);
}
Upvotes: 2