We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Render layouts/useranswers.volt to JSON with parametrs, how to?

I have a layout useranswers.volt and i need render it to a string. I searched on google but found nothing :(

Thanks for any help!

BTW, This way not working for me:

$view = clone $this->view;
        $view->start();
        $view->setVar("answers", $answers);
        $view->setRenderLevel(View::LEVEL_ACTION_VIEW);
        $view->render('layouts', 'useranswers');
        $view->finish();
        $content = $view->getContent();

Unfortunately, this is only for json responses, i need render any layout for json response;

or, can you give me an example?



89

Hi, I'm new to Phalcon and had similar problem (needed to render HTML and put it in JSON response). I did it the following way. Perhaps, it helps.

$this->view->setRenderLevel ( View::LEVEL_ACTION_VIEW );
$this->view->render ( 'layouts', 'useranswers' );
$this->view->finish ();
$this->view->disable ();

$content_html=$this->view->getContent ();
header ( 'Content-type:application/json;charset=utf-8' );
echo "{\"content_html\":" . json_encode ( $content_html) . "}";