Zend framework Zend捕获布局并将内容视为变量
我有一个带有简单示例操作的控制器Mycontroller:Zend framework Zend捕获布局并将内容视为变量,zend-framework,view,Zend Framework,View,我有一个带有简单示例操作的控制器Mycontroller: public function exempleAction(){ // Using layout "mail" $this->_helper->layout()->setLayout("mail"); } 我想使用以下方法获取视图的HTML内容:(以后将其用作电子邮件内容) 这使我能够成功地获取视图内容,但没有布局内容。布局“邮件”的所有HTML代码都不包含在$HTML\u内容中 如何捕获包括布局部分
public function exempleAction(){
// Using layout "mail"
$this->_helper->layout()->setLayout("mail");
}
我想使用以下方法获取视图的HTML内容:(以后将其用作电子邮件内容)
这使我能够成功地获取视图内容,但没有布局内容。布局“邮件”的所有HTML代码都不包含在$HTML\u内容中
如何捕获包括布局部分在内的全部内容 试试这个:
//this will get the current layout instance
//clone it so you wont see any effects when changing variables
$layout = clone(Zend_Layout::getMvcInstance());
//if you want to use another layout script at another location
//$path = realpath(APPLICATION_PATH . '/../emails/');
//$layout->setLayoutPath($path);
//set the layout file (layout.phtml)
//$layout->setLayout('layout');
//prevent this layout from beeing the base layout for your application
$layout->disableLayout();
//get your view instance (or create a new Zend_View() object)
$view = $this->view; //new Zend_View();
//set the path to view scripts if newly created and add the path to the view helpers
//$view->setBasePath(realpath(APPLICATION_PATH.'/../application/emails')."/");
//$view->addHelperPath(realpath(APPLICATION_PATH.'/../application/layouts/helpers/')."/", 'Application_Layout_Helper');
//set some view variables if new view is used (used in the view script $this->test)
$view->assign('test', 'this can be your value or object');
//set the content of your layout to the rendered view
$template = 'index/index.phtml';
$layout->content = $view->render($template);
$bodyHtml = $layout->render();
Zend_Debug::dump($bodyHtml);
或
在你的行动中得到回应。
这将存储通常作为响应发送到浏览器的html
//first disable output if needed
$this->view->layout()->disableLayout();
//get the response object
$response = $this->getResponse();
$bodyHtml = $response->getBody();
Zend_Debug::dump($bodyHtml);
玩得开心 如果我没有弄错的话,在
$view\u helper->action('example','Mycontroller','mymodule')之后没有布局是正常的代码>
实际上,布局是在Zend\u layout\u Controller\u Plugin\u layout
的插件的postDisatch()中调用的
您仍然可以尝试以下方法:
在布局“mail.phtml”中,放置以下内容:
echo $this->layout()->content;
在您的方法中:
$view_helper = new Zend_View_Helper_Action();
$html_content = $view_helper->action('exemple', 'Mycontroller','mymodule');
$layout_path = $this->_helper->layout()->getLayoutPath();
$layout_mail = new Zend_Layout();
$layout_mail->setLayoutPath($layout_path) // assuming your layouts are in the same directory, otherwise change the path
->setLayout('mail');
// Filling layout
$layout_mail->content = $html_content;
// Recovery rendering your layout
$mail_content = $layout_mail->render();
var_dump($mail_content);
$view_helper = new Zend_View_Helper_Action();
$html_content = $view_helper->action('exemple', 'Mycontroller','mymodule');
$layout_path = $this->_helper->layout()->getLayoutPath();
$layout_mail = new Zend_Layout();
$layout_mail->setLayoutPath($layout_path) // assuming your layouts are in the same directory, otherwise change the path
->setLayout('mail');
// Filling layout
$layout_mail->content = $html_content;
// Recovery rendering your layout
$mail_content = $layout_mail->render();
var_dump($mail_content);