2

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?

Jack
  • 31
  • 7
  • Must not have understood the question. Are you trying to get just the contents of the template? Or do you want the template to be accessed globally? – Mike Purcell Apr 26 '12 at 23:54
  • Hi, Mike.. Thanks for answering... The thing is that I need the generated html, that is, the contents of default.phtml – Jack Apr 27 '12 at 00:00
  • So you want the generated contents of the template, interpolated with template variables set in action, and store the render (as if you viewed it in browser) in a variable? – Mike Purcell Apr 27 '12 at 00:05
  • Yes, more or less, I don't get the interpolation part. Actually, due to the fact that the request is via ajax, the default layout is disabled. What I needed was to grab the html content of that layout either way because inside the action I change the default layout (content, title, etc) and send it with the html generated in the action called by the ajax. So I can send 2 separated htmls in one ajax call. – Jack Apr 27 '12 at 18:42

2 Answers2

3

You can stop the rendering and grab the output like this:

$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$this->view->addScriptPath(APPLICATION_PATH . '/layouts/scripts/'); //default layout path
$htmlDefaultLayout = $this->view->render('default.phtml');
Starx
  • 77,474
  • 47
  • 185
  • 261
  • Thanks Starx. That was actually my first try but the problem is that there is no path to default.phtml. – Jack Apr 27 '12 at 18:33
2

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.

drew010
  • 68,777
  • 11
  • 134
  • 162