2016-06-28 16 views
0

Die Frage ist, wie man einen JSON an einen Zweig übergibt, um eine Vorlage zu rendern. Ich habe versucht, einen Json mit JsonResponse-Objekt übergeben, aber ich habe nicht die Möglichkeit gefunden, die Vorlage zu rendern.Wie man Json zu einem Zweig in Symfony2 rendern kann

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 %} 

Wie kann ich eine json von der Steuerung übergeben es in der Datentabelle (in Zweig Vorlage) zu verwenden, aber mantaining das machen in diesem Controller. Können Sie mir bitte helfen?

+0

Können Sie bitte Ihre Code-Beispiele einkochen, bis Sie zeigen nur die notwendigen Teile? – Lumen

+0

Ich verstehe es nicht ... Müssen Sie eine JSON-Codierungszeichenfolge als Antwort von Aktion zurückgeben oder möchten Sie ein JSON-Objekt als Datenanbieter in Ihrer Vorlage verwenden? – Muriano

+0

Ja, ich möchte einen JSON als Datenprovider verwenden, aber ich weiß nicht, wie man das vom Controller macht, um eine Vorlage zu rendern. – jmunozco

Antwort

2

Ich verstehe nicht, was Sie erreichen wollen, aber Sie können einfach json codiert und normale Daten auf Ihren Zweig, senden, es zu benutzen, wie Sie wollen

return $this->render('admin/tarifas/index.html.twig', array(
    'tarifas' => $tarifas 
    'json_tarifas' => json_encode($tarifas) 
)); 
Verwandte Themen