2010-07-20 63 views
3

我想爲我發送的電子郵件使用佈局。我目前使用Zend Layout作爲網頁,但也想主題我的電子郵件。如何在Zend Framework中使用電子郵件的佈局

這是我試過的。

這是我的函數發送電子郵件

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

電子郵件佈局僅僅是

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

當談到因爲通過它只是有一個電子郵件...

The email content 

回答

5

你在你原來的方法非常接近;但是,你必須在Zend_Layout執行對額外的步驟,以得到你想要的東西:

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

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

Zend_Layout::disableLayout()呼叫阻止佈局呈現的直接輸出,並允許您而是呈現佈局存儲在$html變量。然後,您必須手動將Zend_View模板的渲染存儲到Zend_Layout::content變量中。之後,你就定了。

您還可以使用Zend_Layout::setView在佈局中設置Zend_View的實例,以便在渲染布局時可以訪問查看變量。你也可以寫一個視圖幫手來照顧這個。

+0

如果您打算需要原始模板來顯示常規MVC輸出,您可以簡單地克隆佈局以不覆蓋內容/佈局/參數,請使用 '$ layout = clone Zend_Layout :: getMvcInstance();' 作爲第一行 – DesertEagle 2013-11-22 08:56:59

0

您是否考慮過使用Zend_View來生成您的電子郵件模板?

+0

它使用Zend_View作爲電子郵件模板。我想要一個主模板,我注入一個特定的模板,就像Zend_Layout爲網頁所做的一樣。 – 2010-07-27 15:28:23

+0

嗯,我問,因爲在這種情況下,你需要在獨立模式下使用佈局組件:http://framework.zend.com/manual/en/zend.layout.quickstart.html – Chris 2010-07-27 20:26:50

+0

最後,我只用Zend_View和'<?php echo $ this-> render('mail_header.phtml');在內容和<<?php echo $ this-> render('mail_footer.phtml')之前?在內容之後。就像PHP頁眉和頁腳的好日子包括:) – 2010-09-04 19:06:41

相關問題