9

我想创建一个自定义类来生成 HTML 电子邮件。我希望电子邮件的内容来自“电子邮件视图脚本”目录。所以概念是我可以创建一个 HTML 电子邮件视图脚本,就像我创建一个普通的视图脚本一样(能够指定类变量等),并且视图脚本将呈现为电子邮件的 HTML 正文。

例如,在控制器中:

$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
}

然后将呈现 special-welcome-message.text.phtml 和 special-welcome-message.html.phtml 脚本:

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

如何从视图脚本或控制器外部调用局部视图助手?我以正确的方式接近这个吗?或者这个问题有更好的解决方案吗?

4

1 回答 1

12

关于什么:

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

于 2010-07-17T12:04:47.337 回答