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

走远了吗. 提交于 2019-12-12 18:52:05

问题


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?


回答1:


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.




回答2:


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');


来源:https://stackoverflow.com/questions/10342947/how-capture-the-default-phtml-in-a-variable-inside-a-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!