evilReiko
evilReiko

Reputation: 20473

php/zend/json: How to fetch view then pass it as json object?

On the view, there is this basic javascript/jquery:

$('#jsoncallbtn').click(function() {
     $.post('/mycontroller/json', {
          someint: 123,
          somestr: 'string'
     }, function(datafromserver) {
          alert(datafromserver.data1); // prints "test"
          alert(datafromserver.data2); // prints "null"
     }, "json");
});

On server side:

public function jsonAction()
{
    $jsonArray = array('data1' => 'test',
                       'data2' => $this->render('anotheraction'));
    $this->_helper->json($jsonArray);
}

Is there a way to render another action view and send it back for javascript as part of json object?

Upvotes: 0

Views: 783

Answers (2)

Pete
Pete

Reputation: 2246

You could create a separate Zend View instance and add the view's output to the JSON array. E.g something on along the lines of:

$view = new Zend_View();
$view->variable = "testing 123";
$html = $view->render('path/to/view/file.phtml');
$jsonArray["html"] = $html;
Zend_Json::encode($jsonArray);

Upvotes: 1

David Weinraub
David Weinraub

Reputation: 14184

Sounds like the Action View-Helper could do the job.

Upvotes: 2

Related Questions