开发者

get rendered content in an action for ajax response

every body!

I am new to zend framework, I am working on ajax based tabs in zend framework. On clicking a tab I want am calling an action, I want to get rendered content(content rendered in respective view) to send it as response.

Thanks!开发者_如何学Go


Not sure what you want exactly, but you can disable rendering of the layout in your action and have only action view script rendered, e.g.

public function exampleAction() {

    if ($this->getRequest()->isXmlHttpRequest()) {

        $this->_helper->layout->disableLayout();

        $this->view->var = 'some var';
    } else {
        throw new Exception('Not an ajax requrests');
    }
}


View:

<a href="#" class="tab">Click</a>
<div id="content"></div>

JS:

$('.tab').click(function() {
    $.get('/controller/ajax', function(data) {
        $('#content').html(data);
    });
});

Controller:

public function ajaxAction()
{
    echo 'string';
    exit;
}

You just have to add exit; in your action so it doesn't try to render the layout again.


public function yourAction() {
        // get response and layout 
        $response                        = $this->getResponse();
        $layout                          = $this->_helper->layout();

        // rendering action's template      
        $this->render( 'template' );   

        // setting content, don't remember to echo $this->Layout()->content in the layout script                           
        $layout->content                 = $response->getBody();

        //  here you can get your rendered content, and do something with it 
        $renderedContent                 = $layout->render( 'layout' );  

        // you have to clean response otherwise will be automaticaly sent
        $response->clearBody();                                         
}


try this, also make sure to add an exit at the end

$this->view->assign(array('name' => 'my name', 'msg' => 'my message'));
echo $this->view->render('message/display.phtml');
exit;

then in the .phtml file simply call

<?php echo $this->msg; ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜