Sla*_*vic 6 zend-framework capture zend-view
问题:有时在我们的zend控制器中,我们不希望脚本直接输出,而是想要该脚本的内容.一个例子:当我们需要结果时,视图脚本的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);
Run Code Online (Sandbox Code Playgroud)
我希望这很清楚,对某人有用.
尝试使用
public myAction () {
$this->_helper->json(array(
'html' => $this->view->render('projects/climate.phtml'),
'initData'=> 'my other needed data',
));
}
Run Code Online (Sandbox Code Playgroud)
Json行动助手通常会这样做
viewRendererjson_encode 数组| 归档时间: |
|
| 查看次数: |
5147 次 |
| 最近记录: |