php - How to render json into a twig in Symfony2 -
the question how pass json twig render template. i've tried pass json jsonresponse object did not find way render template.
tarifascontroller.php
<?php /* * file part of symfony package. * * (c) fabien potencier <fabien@symfony.com> * * full copyright , license information, please view license * file distributed 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> {% 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 %}
how can pass json controller use in datatable (in twig template) mantaining render in controller. please, can me?
i don't understand trying achieve can send json encoded , normal data twig, use how want
return $this->render('admin/tarifas/index.html.twig', array( 'tarifas' => $tarifas 'json_tarifas' => json_encode($tarifas) ));
Comments
Post a Comment