Zend capture layout and view content as variable - zend-framework

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!

Related

zend framework 3 how can call phtml file in controller

I want to get html content in controller .
for this
public function posteemailAction(){
$view=$this->render("auth-acl/email/_getemail.phtml");
print_r($view);die;
}
Can anyone help for getting content of html in controller.
Thanks
Try this:
public function ajaxAction() {
// Turns off the layout and the view
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
// Creates a new instance of Zend_View
$html = new Zend_View();
// Set a script path for above view
$html->setScriptPath(APPLICATION_PATH . '/views/scripts/your_custom_path/');
// Assinging some data to the view
$html->assign('myVar', $someValue);
// Renders the view to specified variable
$responseContent = $html->render('mycontent.phtml');
echo $responseContent;
}

zend form elements display hidden field value

$emailmessage = new Zend_Form_Element_Hidden('emailmessage');
the hidden filed value, i.e 'emailmessage' retrieves the value of the same field name inside the database. However on loading the page, value of the 'emailmessage' cannot be seen, since the element is hidden.
Is there any way to display it without using any other form elements. I want it without using text, textarea, etc.
in controller you need to assign it to view and in the view you can echo it where ever you want:
controller
$form = new Your_Form();
$this->view->emailmessage = $emailmessage;
View
echo $this->escape($this->emailmessage);
You need to set formNote decorator for the element.
You do it by extending Hidden element or by setting decorator in the form.
Form
public function init()
{
// ...
$emailMessage = new Zend_Form_Element_Hidden();
$emailMessage->setDecorators(
array(
array('ViewHelper', array('helper' => 'formNote'))
)
);
$this->addElement($emailMessage, 'emailMessage');
// ...
}

Zend_Pdf and templated

I have problems creating a template for my pdf documents in Zend_Pdf. I've followed the guide provided at http://framework.zend.com/manual/en/zend.pdf.pages.html but it doesn't seem to work since no footer text or logo are visible at any page except the first.
This is a sample of my code: (Note; the MTP constant is just the recalculate from mm to points)
//Create pdf object
$pdf = new Zend_Pdf();
//Create the first page
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
/*HEADER*/
//insert logo at the top
$logo = Zend_Pdf_Image::imageWithPath(APPLICATION_PATH . '/../document_root/images/logotype.png');
$page->drawImage($logo, 22*MTP,274*MTP, 62*MTP, 289*MTP);
/*FOOTER*/
$footerStyle->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 10);
$page->setStyle($footerStyle);
$page->drawText('Footer text', 33*MTP, 20*MTP, 'ISO-8859-1');
//save the page to pdf->pages
$pdf->pages[0] = $page;
//save the page as a template
$template = $pdf->pages[0];
//create the next page from template
$page1 = new Zend_Pdf_Page($template);
//save the next page to pdf->pages
$pdf->pages[] = $page1;
Have I completely misunderstood how this "template function" work in Zend_Pdf? I know that Zend_Pdf lacks quite a number of features compaired to some of the external pdf-generators (like FPDF) but still, there must be some way to make a basic header/footer for all pages right?
Try to use clone instead passing page as argument to a new page.
$page1 = clone $template;
or simply
$pdf->pages[] = clone $pdf->pages[0];

Way to define others layout() parts such layout()->content?

Ive done some search but no success.. i am trying to figure out how to define others layout() parts such layout()->content variable.. i would love to get int layout()->navigation (a custom one) which display the navigation..
Any ideas ?
Thanks.
Not sure if this is what you want, but you can create additional 'parts' of layout simply by assigning a value to your new part. ZF will take care of the rest. For example, in a bootstrap.php you could do:
public function _initNewLayoutPart() {
$view = $this->bootstrap('view')->getResource('view');
$view->layout()->newpart = 'some new part';
}
Then in your layout.phtml you could just echo the new part:
<?php echo $this->layout()->newpart; ?>
It is possible by just creating a new variable in layout, you can define it in your controller (preferably in init or postDispatch). Just like this:
public function init()
{
$this->view->layout()->motd = '<b>Message of the day.</b>';
}
Then in your actual view where you want to see the message, all you have to do is:
<?php echo $this->layout()->motd; ?>
If you want something fancier, such as rendering a whole page or sidebar, try the following:
public function init()
{
$this->view->layout()->sidebar = $this->view->action('render', 'sidebar');
}
With render being the action (including render.phtml) and sidebar being the controller.

set/add view from controller action

I'm trying to set a view script to be executed in addition to the currently requested action view script. I want to do this from the controller action itself in a way that this new view script output will be available from the layout $this->layout()->content helper.
I found the setView() method but don't know how to use it from the controller.
Thanks a lot.
If you just want to render some other view script from a controller just:
$this->render('someotherview');
Wich will render someotherview.phtml.
from: http://framework.zend.com/manual/en/zend.controller.action.html#zend.controller.action.viewintegration.render
class MyController extends Zend_Controller_Action{
public function fooAction()
{
// Renders my/foo.phtml
$this->render();
// Renders my/bar.phtml
$this->render('bar');
// Renders baz.phtml
$this->render('baz', null, true);
// Renders my/login.phtml to the 'form' segment of the
// response object
$this->render('login', 'form');
// Renders site.phtml to the 'page' segment of the response
// object; does not use the 'my/' subirectory
$this->render('site', 'page', true);
}
public function bazBatAction()
{
// Renders my/baz-bat.phtml
$this->render();
}
}
Should get you on the right track!
Also
$this->renderScript('path/to/index.phtml');
Works really well.