3

I would like to use a layout for the emails I'm sending out. I'm currently usine Zend Layout for the web pages, but would like to theme my emails as well.

Here is what I've tried.

This is my function that sends the email

    $layout = Zend_Layout::getMvcInstance();
    $this->_view->render($template);
    $html = $layout->render('email');
    $this->setBodyHtml($html,$this->getCharset(), $encoding);
    $this->send();

The email layout is simply

    The email content
    <?php echo $this->layout()->content; ?>

When it comes through as an email it just has...

    The email content
Shane Stillwell
  • 3,198
  • 5
  • 31
  • 53

2 Answers2

5

You're pretty close in your original method; however, you have to perform a couple extra steps on Zend_Layout to get what you want:

$layout = Zend_Layout::getMvcInstance();
$layout->setLayout('email')
    ->disableLayout();
$layout->content = $this->_view->render($template);
$html = $layout->render();

$this->setBodyHtml($html, $this->getCharSet(), $encoding)->send();

The call to Zend_Layout::disableLayout() prevents direct output of the layout rendering and allows you instead to store the rendered layout in the $html variable. Then, you have to manually store the rendering of the Zend_View template in into the Zend_Layout::content variable. After that, you're set.

You can also use Zend_Layout::setView to set an instance of Zend_View within the layout so that when you render the layout, you can get access to view variables. You could probably also write a view helper to take care of this.

GuySMiLEZ
  • 111
  • 1
  • 3
  • if you are going to need the original template to display the regular MVC output, you can simply clone the layout to not override the content/layout/parameters, use `$layout = clone Zend_Layout::getMvcInstance();` as the first line – DesertEagle Nov 22 '13 at 08:56
0

Have you considered just using Zend_View to generate your email template?

Chris
  • 884
  • 5
  • 8
  • It is using Zend_View for the email template. I want to have a master template that I inject a specific template into, exactly like Zend_Layout does for web pages. – Shane Stillwell Jul 27 '10 at 15:28
  • Well I'm asking because in that case you need to use the Layout component in standalone mode: http://framework.zend.com/manual/en/zend.layout.quickstart.html – Chris Jul 27 '10 at 20:26
  • In the end, I'm just using Zend_View with `render('mail_header.phtml'); ?>` before the content and `render('mail_footer.phtml'); ?>` after the content. Just like the good old days of PHP header and footer includes :) – Shane Stillwell Sep 04 '10 at 19:06