问题:有时在我们的 Zend Controller 中,我们不希望直接输出脚本,而是希望该脚本的内容。一个示例:当我们需要将 View 脚本的结果 html 输出包含在另一种结构(如 JSON 或 XML)中以便在客户端进行处理时。
我在堆栈溢出时找到了结果,但没有那么快,因为它在不同的上下文中。我已经为此苦苦挣扎了 2 天了。事实证明,这非常简单:
// in our controllers' action method
$this->_helper->layout()->setLayout('empty'); // disable layout
$this->_helper->viewRenderer->setNoRender(true); // make sure the script is not being rendered
// any of your code here
$html = $this->view->render('projects/climate.phtml'); // return the view script content as a string
$json = array('html'=>$html, 'initData'=>'my other needed data');
echo json_encode($json);
我希望这很清楚并且对某人有用。
最佳答案
尝试使用
public myAction () {
$this->_helper->json(array(
'html' => $this->view->render('projects/climate.phtml'),
'initData'=> 'my other needed data',
));
}
Json Action 助手通常会
viewRenderer
json_encode
数组 关于zend-framework - 如何捕获 Zend View 输出而不是实际输出它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3805512/