使用子视图渲染视图?
Render a view with a child view?
我正在尝试发送带有特定模板(电子邮件的主视图,例如 2 栏布局等)的特定 "layout"(针对 header/footer)的电子邮件。
$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');
好的,我成功了! :) 我必须渲染 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;
}
我正在尝试发送带有特定模板(电子邮件的主视图,例如 2 栏布局等)的特定 "layout"(针对 header/footer)的电子邮件。
$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');
好的,我成功了! :) 我必须渲染 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;
}