0
問題是如何將json傳遞給樹枝來渲染模板。我試圖通過與JsonResponse對象的JSON,但我沒有找到方式來呈現模板。如何在Symfony2中將json渲染成樹枝
TarifasController.php
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Controller\Admin;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use AppBundle\Entity\Tarifa;
/**
*
* @Route("/tarifas")
*/
class TarifasController extends Controller
{
/**
* @Route("/", name="tarifa_index")
*/
public function indexAction()
{
$entityManager = $this->getDoctrine()->getManager();
$tarifas = $entityManager->getRepository('AppBundle:Tarifa')->findAll();
return $this->render('admin/tarifas/index.html.twig', array('tarifas' => $tarifas));
//return new JsonResponse(array('json_tarifas' => json_encode($tarifas)));
}
}
index.html.twig
{% extends 'admin/layout.html.twig' %}
{% block body_id 'admin_post_show' %}
{% block main %}
<h1>{{ 'label.project_list'|trans }}</h1>
<table id="tarifas_index" class="table table-striped">
<thead>
<tr>
<th>{{ 'Id' }}</th>
<th><i class="fa fa-user"></i> {{ 'label.title'|trans }}</th>
<th><i class="fa fa-calendar"></i> {{ 'label.summary'|trans }}</th>
<th><i class="fa fa-calendar"></i> {{ 'label.content'|trans }}</th>
<th><i class="fa fa-cogs"></i> {{ 'label.actions'|trans }}</th>
</tr>
</thead>
<tbody>
{% for tarifa in tarifas %}
<tr>
<td>{{ tarifa.id }}</td>
<td>{{ tarifa.codigo }}</td>
<td>{{ tarifa.nombre }}</td>
<td>{{ tarifa.razon }}</td>
<td>
<div class="item-actions">
<a href="{{ path('project_detail', { id: tarifa.id }) }}" class="btn btn-sm btn-default">
{{ 'action.show'|trans }}
</a>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
$(document).ready(function() {
$('#tarifas_index').DataTable({
data: tarifas
});
});
</script>
{% endblock %}
我如何傳遞一個JSON從控制器DataTable中使用它(在樹枝模板),但mantaining渲染在這個控制器中。請,你能幫我嗎?
能否請您歸結您的代碼示例,直到他們只顯示必要的部分? – Lumen
我不明白它......你需要返回一個JSON編碼字符串作爲操作的響應,還是你想在模板中使用JSON對象作爲數據提供者? – Muriano
是的,我想使用json作爲數據提供程序,但我不知道如何從控制器執行此操作來呈現模板 – jmunozco