0

I am trying to send an email with a specific "layout" (for header/footer) with a specific template (main view of the email, e.g. 2-column layout etc.).

The $mainView html has: <?= $this->content ?> but this is always NULL when it should be the $childView html with all variables.

Here is what I have in a MailService.php file:

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 is injected in the Factory:

/** @var RendererInterface $viewRenderer */
$viewRenderer = $serviceLocator->get('viewrenderer');
dan2k3k4
  • 1,388
  • 2
  • 23
  • 48
  • I would like to help, I just checked my working codes, and I realize I don't use childView in it!!! Sorry! (ZF2 is so hard, it's a real pain! whenever I see a problem like this, I remember why I dump it for Meteor! True story!) – Mehdi Sabouri Mar 30 '16 at 16:39

1 Answers1

1

Ok I got it working ! :) I had to render the childView then set this render as the view variable for the 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;
}
dan2k3k4
  • 1,388
  • 2
  • 23
  • 48
  • 3 min too late sorry, yes as you noticed child view models should not use terminal since its in consensu with the pattern. When I tested it i actually got an error have you turned off some of the error reporting? – KatsuoRyuu Mar 31 '16 at 07:46
  • Hmm, I think I have default error reporting, I can try and see if I get any errors but I know it works for me :s – dan2k3k4 Mar 31 '16 at 08:28