Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a mail template with layout in ZF2?

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;
}
like image 846
user1986560 Avatar asked Jan 17 '13 09:01

user1986560


Video Answer


1 Answers

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);
like image 157
user1986560 Avatar answered Sep 30 '22 22:09

user1986560