In the ZF1 I used the following code to render a mail body:
// View erstellen
$view = new Zend_View();
// Layout erstellen
$layout = new Zend_Layout();
// HelperPath muss hier nochmals übergeben werden da es ein neues View Objekt ist.
$view->addHelperPath('Own/View/Helper', "Own_View_Helper_");
// ViewScript
$view->setScriptPath(APPLICATION_PATH . '/views/scripts/emails/');
// LayoutPath
$layout->setLayoutPath(APPLICATION_PATH . '/layouts/scripts/');
$layout->setLayout('layoutMail');
$layout->setView($view);
foreach ($assigns as $key => $value) {
$view->assign($key,$value);
}
$layout->content = $view->render($templateName);
return $layout->render();
I tried a lot but I cannot realise this function in ZF2. My actual code is this. But it uses the standard layout and I cannot get it in a string.
public function mailAction()
{
$viewModel = new ViewModel();
$viewModel->setTemplate("zhorty/test");
$viewModel->setVariable('test', 'some value');
return $viewModel;
}
Thank you! Your answer helped me!
I extended the code. So I can use a layout template as well.
$view = new \Zend\View\Renderer\PhpRenderer();
$resolver = new \Zend\View\Resolver\TemplateMapResolver();
$resolver->setMap(array(
'mailLayout' => __DIR__ . '/../../../../Application/view/layout/layout-mail.phtml',
'mailTemplate' => __DIR__ . '/../../../view/zhorty/test.phtml'
));
$view->setResolver($resolver);
$viewModel = new \Zend\View\Model\ViewModel();
$viewModel->setTemplate('mailTemplate')
->setVariables(array(
'test' => 'AlloVince'
));
$content = $view->render($viewModel);
$viewLayout = new \Zend\View\Model\ViewModel();
$viewLayout->setTemplate('mailLayout')
->setVariables(array(
'content' => $content
));
echo $view->render($viewLayout);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With