I have a controller action that triggers a Zend\EventManager\Event
to which I have attached a listener that calls a service to do the heavy lifting of sending an email message. I would like to be able to set the layout, then set the rendered view ouput as the message body. There is no problem getting my multipart/alternative email together. The problem is rendering the HTML content.
It happens to be a new user registration scenario, so the AccountManager class goes
public function onRegistrationSubmitted(EventInterface $event)
{
// do stuff
}
and in that method body I have tried things like
$controller = $event->getTarget();
$controller->layout()->setTemplate(
'my/email/layout.phtml'
);
followed by
$view = new ViewModel();
$view->setTemplate('my/email/view_script.phtml');
$html = $this->viewRenderer->render($view);
// P.S: $this->viewRenderer comes from the service manager,
// and is injected by our factory
but the result is the layout, fine, but the nested $this->content
is null.
Then I tried creating my own layout and child, as it were, by doing
$layout = (new ViewModel())->setTemplate( 'my/email/layout.phtml');
$child = (new ViewModel())->setTemplate( 'my/email/view_script.phtml');
$layout->addChild($child,'content');
but when we look at $html = $this->viewRenderer->render($layout)
it’s the same story: no nested content, just layout. Conversely, if I do $html = $this->viewRenderer->render($child)
then I get the $child without the surrounding layout (which is what I’d expect, but hey, I am trying to leave no stone unturned).
I know of at least one way to work around this: do without the layout, just have a flat viewscript. But I am stubborn.
Any thoughts?