luca.p.alexandru
luca.p.alexandru

Reputation: 1750

Zend framework render html

For a Zend framework project, which model is it best to render html code? The model or the controller (If it can't be done in the view, for instance let's say we have an ajax request)

Upvotes: 1

Views: 1005

Answers (2)

KumarA
KumarA

Reputation: 1378

You can use ViewModel from Controller in ZendFramework 2 as below in your action:

$data_to_view = new ViewModel('ARRAY OF DATA TO RENDER');
return $data_to_view ;

Upvotes: 0

Daniel Gadawski
Daniel Gadawski

Reputation: 1923

I think you can still use Zend_View. If you need some content by an AJAX request, just render it in a controller and then pass it as an response (by simple echo'ing it). Example:

// controller body

public function ajaxAction()
{
    // turn off the layout and view
    $this->_helper->layout()->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(true);

    // create new instance of Zend_View
    $html = new Zend_View();

    // set script path for above view
    $html->setScriptPath(APPLICATION_PATH . '/views/scripts/_custom/');

    // assing some data to view
    $html->assign('myVar', $someValue);

    // render view to specified variable
    $responseContent = $html->render('mycontent.phtml');

    echo $responseContent;
}

Now create application/view/scripts/_custom/mycontent.phtml script just like other views to suite your needs.

Upvotes: 2

Related Questions