How to retrieve the contents of a rendered template in Symfony2 and store it in a String

0

I'm using Symfony2 , I'm still getting familiar with this framework. And while using my control I'm not sure how to get the output generated by it and play to a string. I need to play to a string to handle, as I'm using pjax and if the request is a request originating from pjax I need to send only a snippet of the page (the content I want to modify), to do this I plan to use the Crawler and then return only the correct content, depending on the situation. The problem is that I do not know how to do how to capture the HTML that the Controller generates.

<?php
/**
 * State controller.
 *
 * @Route("/state")
 */
class StateController extends Controller {

    /**
     * Lists all State entities.
     *
     * @Route("/", name="state")
     * @Method("GET")
     * @Template()
     */
    public function indexAction() {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('AmbulatoryBundle:State')->findAll();

        $rendered = $this->render('TestBundle:State:index.html.twig', array(
            'entities' => $entities,
        ));

        return $rendered; // Preciso pegar o conteúdo gerado aqui em string
    }

? >

    
asked by anonymous 26.07.2014 / 19:18

1 answer

1

SF2 returns the object \Symfony\Component\HttpFoundation\Response in its controller .

For you to have access to string Html, you can do this:

<?php
/**
 * State controller.
 *
 * @Route("/state")
 */
class StateController extends Controller {

    /**
     * Lists all State entities.
     *
     * @Route("/", name="state")
     * @Method("GET")
     * @Template()
     */
    public function indexAction() {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('AmbulatoryBundle:State')->findAll();

        $response = $this->render('TestBundle:State:index.html.twig', array(
            'entities' => $entities,
        ));

        $html = $response->getContent(); // Faça algo com a string....

        return $response;
    }

More details on Object Doc

    
26.07.2014 / 20:57