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

Asked

Viewed 147 times

0

I’m using the Symfony2, I’m still getting familiar with this framework. And when using my controller I’m not sure how to pick up the output generated by it and play for a string. I need to play for a string to treat because I’m using the pjax and if the request is a request originated by pjax i need to send only a snippet of the page (the content I want to modify), to do this I intend to use the Crawler and thus return only the correct content, depending on the situation. The problem is that I don’t know 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
    }

?>

1 answer

1


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

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

<?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 Doc of the object

Browser other questions tagged

You are not signed in. Login or sign up in order to post.