是否有可能在变量中获取Phalcon \ Mvc \ View呈现的输出? [英] It is possible to get Phalcon\Mvc\View rendered output in variable?
本文介绍了是否有可能在变量中获取Phalcon \ Mvc \ View呈现的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要还给json对象,该对象具有带有渲染操作的属性"html". 可以用Phalcon vew进行本机处理吗?
I need to give back json object, that has property 'html' with rendered action. Is it possible to do natively with Phalcon vew?
示例:
$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.有关phalcon php框架的问题: http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View.html
P.S. Question regarding phalcon php framework: http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View.html
推荐答案
首先需要启动输出缓冲:
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',
....
];
这篇关于是否有可能在变量中获取Phalcon \ Mvc \ View呈现的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文