How to capture a Zend view output instead of actually outputting it

廉价感情. 提交于 2019-12-07 02:44:30

问题


Problem: sometimes in our zend controller we don't want the script to be output directly, but rather want the content of that script. One example: when we need the result html output of a view script be included in another structure like JSON or XML for processing in the client side.

I found the result here at stack overflow, but not so quick since it was in a different context. I have been struggling with this for 2 days now. As it turned out it was very simple:

    // 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);

I hope this was clear and will be useful to somebody.


回答1:


Try using

public myAction () {
    $this->_helper->json(array(
        'html'    => $this->view->render('projects/climate.phtml'),
        'initData'=> 'my other needed data',
    ));
}

The Json action Helper will normally

  • disable the viewRenderer
  • disable the layout
  • json_encode the array


来源:https://stackoverflow.com/questions/3805512/how-to-capture-a-zend-view-output-instead-of-actually-outputting-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!