我正在尝试发送一封包含特定"布局的电子邮件" (对于页眉/页脚)具有特定模板(电子邮件的主视图,例如2列布局等)。
$mainView
html有:<?= $this->content ?>
但是当它应该是包含所有变量的$childView
html时,它总是为NULL。
以下是MailService.php
文件中的内容:
public function createMessage($to, $subject, $template, $variables) : Message
{
// Create mail
$message = new Message();
// Create mail body
$mainView = new ViewModel();
$mainView->setTerminal(true);
$mainView->setTemplate('layout/mail.phtml');
$childView = new ViewModel();
$childView->setTerminal(true);
$childView->setTemplate($template);
$childView->setVariables($variables);
$mainView->addChild($childView, 'content');
$msgRender = $this->viewRenderer->render($mainView);
$body = new Part($msgRender);
$body->setType(Mime::TYPE_HTML);
$messageBody = new MessageBody();
$messageBody->addPart($body);
$message->setBody($messageBody);
$message->setTo($to);
$message->setSubject($subject);
return $message;
}
$this->viewRenderer
注入工厂:
/** @var RendererInterface $viewRenderer */
$viewRenderer = $serviceLocator->get('viewrenderer');
答案 0 :(得分:0)
好的,我搞定了! :)我必须渲染childView
然后将此渲染设置为mainView
的视图变量
public function createMessage($to, $subject, $template, $variables) : Message
{
// Create mail
$message = new Message();
// Create mail body
$mainView = new ViewModel();
$mainView->setTerminal(true);
$mainView->setTemplate('layout/mail.phtml');
$childView = new ViewModel();
$childView->setTemplate($template);
$childView->setVariables($variables);
$childRender = $this->viewRenderer->render($childView); // render child
$mainView->setVariable('content', $childRender); // set childRender to mainView
$msgRender = $this->viewRenderer->render($mainView);
$body = new Part($msgRender);
$body->setType(Mime::TYPE_HTML);
$messageBody = new MessageBody();
$messageBody->addPart($body);
$message->setBody($messageBody);
$message->setTo($to);
$message->setSubject($subject);
return $message;
}