我想返回一个 HTML 片段以及 json 字符串中的其他值,这就是我所拥有的:
$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;
但我得到的只是 {"html":{"headers":{}}}
。有没有办法只抓取呈现的 HTML?
最佳答案
使用 $this->renderView()
代替。
$this->render()
返回一个 Response
对象,而 $this->renderView()
返回一个由渲染产生的字符串一个模板。
关于php - 如何将 Twig 呈现的模板作为 JSON 响应的一部分返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13257156/