2010-07-16 48 views
9

我想創建一個自定義類,它將生成一個HTML電子郵件。我希望電子郵件的內容來自「電子郵件視圖腳本」目錄。所以這個概念將會是我可以創建一個HTML電子郵件視圖腳本,就像創建一個普通的視圖腳本(能夠指定類變量等)一樣,視圖腳本將被呈現爲電子郵件的HTML主體。如何在控制器或視圖外部使用Zend Framework的部分視圖助手?

例如,在控制器:

$email = My_Email::specialWelcomeMessage($toEmail, $firstName, $lastName); 
$email->send(); 

My_Email::specialWelcomeMessage()功能會做這樣的事情:

public static function specialWelcomeMessage($toEmail, $firstName, $lastName) { 
    $mail = new Zend_Mail(); 
    $mail->setTo($toEmail); 
    $mail->setFrom($this->defaultFrom); 
    $mail->setTextBody($this->view->renderPartial('special-welcome-message.text.phtml', array('firstName'=>$firstName, 'lastName'=>$lastName)); 
} 

理想的情況下,這將是最好的,如果我能找到一種方法,使specialWelcomeMessage()功能就像這樣簡單:

public static function specialWelcomeMessage($toEmail, $firstName, $lastName) { 
    $this->firstName = $firstName; 
    $this->lastName = $lastName; 
    //the text body and HTML body would be rendered automatically by being named $functionName.text.phtml and $functionName.html.phtml just like how controller actions/views happen 
} 

Wh然後ICH會呈現特殊的歡迎,message.text.phtml和特殊的歡迎,message.html.phtml腳本:

<p>Thank you <?php echo $this->firstName; ?> <?php echo $this->lastName; ?>.</p> 

我怎麼會叫的局部視圖助手從視圖腳本或控制器之外?我以正確的方式接近這個嗎?或者有更好的解決方案來解決這個問題嗎?

回答

11

什麼:

public static function specialWelcomeMessage($toEmail, $firstName, $lastName) { 
    $view = new Zend_View; 
    $view->setScriptPath('pathtoyourview'); 
    $view->firstName = $firstName; 
    $view->lastName = $lastName; 
    $content = $view->render('nameofyourview.phtml'); 
    $mail = new Zend_Mail(); 
    $mail->setTo($toEmail); 
    $mail->setFrom($this->defaultFrom); 
    $mail->setTextBody($content); 
} 

如果你想要像你說的,爲什麼不使用讓你調用動作的名稱或控制器,並把它作爲一個變量來動態地改變你的操作名稱腳本路徑,還是更好的默認參數。這將有助於:

http://framework.zend.com/manual/en/zend.controller.request.html

相關問題