I have a controller Mycontroller with simple exemple action:
public function exempleAction(){
// Using layout "mail"
$this->_helper->layout()->setLayout("mail");
}
I want to get HTML content of the view using: (to use it later as email content)
$view_helper = new Zend_View_Helper_Action();
$html_content = $view_helper->action('exemple', 'Mycontroller','mymodule');
This successfully allow me to get the view content but WITHOUT the layout content. All the HTML code of the layout "mail" is not included in $html_content.
How can i capture the whole content includind the layout part?
If I'm not mistaken, it is normal that you do not have the layout after $view_helper->action('exemple', 'Mycontroller','mymodule');
Indeed, the layout is call in postDisatch() of Zend_Layout_Controller_Plugin_Layout
's plugin.
You can still try this:
In your layout 'mail.phtml' put this:
echo $this->layout()->content;
In your method :
$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);
Try this:
//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);
Or
Get the response body in your action.
This will store the html that normally would be send to the browser as response.
//first disable output if needed
$this->view->layout()->disableLayout();
//get the response object
$response = $this->getResponse();
$bodyHtml = $response->getBody();
Zend_Debug::dump($bodyHtml);
Have fun!