【问题标题】:How to use the Zend Framework partial view helper outside of a controller or view?如何在控制器或视图之外使用 Zend Framework 部分视图助手?
【发布时间】:2011-03-17 03:43:51
【问题描述】:

我想创建一个自定义类来生成 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>

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

【问题讨论】:

    标签: php email zend-framework partial-views html-email


    【解决方案1】:

    怎么样:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多