Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to get Phalcon\Mvc\View rendered output in variable?

Tags:

php

view

phalcon

I need to give back json object, that has property 'html' with rendered action. Is it possible to do natively with Phalcon vew?

Example:

$posts = NewsPost::find(['limit' => 10]);
$view = new Phalcon\Mvc\View();
$view->setVar('posts', $posts);
$view->setMainView('news/posts'); // not sure if this is correct

// retrieve some data ...
$response = [
    'html' => $view->render(),
    'somedata' => 'somevalues',
    ....
];

P.S. Question regarding phalcon php framework: http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View.html

like image 468
avasin Avatar asked Feb 18 '23 04:02

avasin


1 Answers

The output buffering needs to be started first:

$view = new Phalcon\Mvc\View();

$view->setVar('posts', $posts);

$view->start();
$view->render(); //Pass a controller/action as parameters if required
$view->finish();

// retrieve some data ...
$response = [
    'html' => $view->getContent(),
    'somedata' => 'somevalues',
    ....
];
like image 178
twistedxtra Avatar answered Apr 28 '23 12:04

twistedxtra