Search code examples
zend-view

How can I use Zend Layout with Zend View if I call the view from a model?


Basically, I want to render a view and layout from a model. Don't ask me why.

First of all, the views work as intended and I'm loading them into a variable for my perverse use later on. I am also fully aware that I could always do partial scripts. It seems to be a valid fallback, but it just doesn't cut it.

What I want to do is to get the layout to work automatically just like in the case with controllers and views.

Right now I employ something like this:

// Class blablabla
$layout = new Zend_Layout();
$layout->enableLayout();
$layout->setView($view);

// Ugly url, I know, I'm experimenting and they work
$body = $layout->render('mailer/layout/mail');
$body .= $view->render('mailer/templates/' . $type . '.phtml');  

The problem is that $body contains the layout and only then the actual view. Any advice? What am I doing wrong?


Solution

  • Assuming that your layout contains the default $this->layout()->content somewhere, you'd want this:

    $layout->content = $view->render('...');
    $body = $layout->render('...');
    

    Source: http://www.wowww.ch/2009/03/16/zend-mail-avec-template-standardise-avec-zend-layout-et-zend-view/