Search code examples
zend-frameworkzend-viewzend-layout

How capture the default.phtml in a variable inside a controller


I have a simple question... How could I render the contents of the default.phtml which is in Project/application/layouts/scripts/default.phtml to a variable, so I can have its html.

In the index controller, with an action and a phtml file named test, this would work:

$html = $this->view->render('index/test.phtml');

But, of course, this does not:

$htmlDefaultLayout = $this->view->render('default.phtml');

Since default.phtml is not inside any controller, I guess.

Is there a good way to do that?


Solution

  • You can add to the path that Zend_View looks in for views so you could then render the default.phtml file.

    Example:

    // add the layout directory to the path
    $this->view->addScriptPath(APPLICATION_PATH . '/layouts/scripts/');
    
    $htmlDefaultLayout = $this->view->render('default.phtml');
    

    The last path added to the scriptPath in Zend_View are the first to be checked (LIFO).

    See View Script Paths.