如何在ZF2中呈现带有布局的邮件模板?

时间:2013-01-17 09:29:30

标签: php email templates layout zend-framework2

在ZF1中,我使用以下代码呈现邮件正文:

// 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();

我尝试了很多,但我在ZF2中无法实现这个功能。我的实际代码就是这个。但它使用标准布局,我不能用字符串获取它。

public function mailAction()
{
    $viewModel = new ViewModel();
    $viewModel->setTemplate("zhorty/test");
    $viewModel->setVariable('test', 'some value');
    return $viewModel;
}

5 个答案:

答案 0 :(得分:10)

谢谢!你的回答对我有帮助!

我扩展了代码。所以我也可以使用布局模板。

$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);

答案 1 :(得分:7)

我的回答基于user1986560的回答和How to Render a custom view with plugins in Zend Framework 2。我正在添加它,因为我认为它使事情更容易实现。

我有电子邮件布局和不同的内容文件。可以重用布局并添加不同的内容文件。

查看/电子邮件/ layout.phtml     

<table>
    <tr><td><img src="header.png" /></td></tr>
    <tr><td><?= $this->content; ?></td></tr>
    <tr><td><img src="footer.png" /></td></tr>
</table>

查看/电子邮件/ contact.phtml     

<h1>Contact us email</h1>
</ br>
Name: <?= $this->name;?></ br>
Email: <?= $this->email;?></ br>
Message:<?= $this->message;?></ br>

在您的模块conf中,添加布局和不同的内容文件。通过这种方式,您可以使用视图助手。

<强> module.config.php

'view_manager' => array(
    'template_map' => array(
        'email/layout'  => __DIR__ . '/../view/email/layout.phtml',
        'email/contact' => __DIR__ . '/../view/email/contact.phtml',
    ),

控制器/操作

// View renderer
$renderer = $this->getServiceLocator()->get('Zend\View\Renderer\RendererInterface');

// Email content
$viewContent = new \Zend\View\Model\ViewModel(
    array(
        'name'    => $name,
        'email'   => $email,
        'message' => $message,
));
$viewContent->setTemplate('email/contact'); // set in module.config.php
$content = $renderer->render($viewContent);

// Email layout
$viewLayout = new \Zend\View\Model\ViewModel(array('content' => $content));
$viewLayout->setTemplate('email/layout'); // set in module.config.php

// Email
$html = new MimePart($renderer->render($viewLayout));
$html->type = 'text/html';
$body = new MimeMessage();
$body->setParts(array($html));
$message = new \Zend\Mail\Message();
$message->setBody($body);

答案 2 :(得分:6)

希望我的一篇博文会帮助你,这是关于how to use Zend\View as template in Zend\Mail and add attachments in ZF2

它是由中文编写的,但我认为只需阅读代码就足够清楚了。你也可以read it with help of Google translation

答案 3 :(得分:0)

对于sendig电子邮件,我建议使用以下模块:https://github.com/WasabiLib/Mail

模块基于ZF2消息,并配置为服务。所以你只需要打电话给服务经理。

body方法能够处理ZF2 viewModels。请参阅以下示例:

$mail = $this->getServiceLocator()->get("Mail");
$viewModel = new ViewModel(array('$yourVariable1' => 'yourContent1', $yourVariable2=>'yourContent2',...));
$viewModel->setTemplate("responsive");
$mail->setTo('recipient@domain.com');
$mail->setBody($viewModel);
$mail->send();

它与您可以自定义的自适应电子邮件模板捆绑在一起。 您只需要将模块添加到application.config.php就可以了。

答案 4 :(得分:-1)

@ user1986560答案不完整,因为你必须要一些基本的帮手,比如&#34; url&#34;与路由器有时&#34; basePath&#34;也需要。

这是完整的代码 -

$myApp=Zend\Mvc\Application::init(require 'config/application.config.php');
$sm=$myApp->getServiceManager();

$view=new \Zend\View\Renderer\PhpRenderer();
$view->getHelperPluginManager()->setServiceLocator($sm);
$basePathX='/your-folder-from-where-you-want-to-run';
$request=$myApp->getRequest();
if($request instanceof \Zend\Http\PhpEnvironment\Request){
    #die($request->getBasePath());
    $basePathX=$request->getBasePath();
}
$basePath1=explode('/', $basePathX);
/* please fix this path as your requirement */
array_pop($basePath1);
$bsPath=implode('/', $basePath1).'/';
/* following line will fix your router path, it is important */
$myApp->getMvcEvent()->getRouter()->setBaseUrl($bsPath);
$basePath=new \Zend\View\Helper\BasePath();
$basePath->setBasePath($bsPath);
$view->getHelperPluginManager()->setService('basePath', $basePath);
$urlHlpr=new \Zend\View\Helper\Url();
$urlHlpr->setRouter($myApp->getMvcEvent()->getRouter());
$view->getHelperPluginManager()->setService('url', $urlHlpr);

$resolver=new \Zend\View\Resolver\TemplateMapResolver();
$resolver->setMap(array(
        'wmsLayout' => APP_ROOT_PATH . 'module'.DS.'YourModule'.DS.'view'.DS.'layout'.DS.'layout1.phtml',
        'layout/left-menu' => APP_ROOT_PATH . 'module'.DS.'YourModule'.DS.'view'.DS.'layout'.DS.'left-menu.phtml',
));
$view->setResolver($resolver);

$viewModel = new \Zend\View\Model\ViewModel();
$tmpltFileToLoad=__DIR__ . DS.'your-file-to-render.phtml';
$tmpltIdx='_'.md5($tmpltFileToLoad);
$resolver->add($tmpltIdx, $tmpltFileToLoad);
$viewModel->setTemplate($tmpltIdx)->setVariables(array(
        'test' => 'shahadat hossain khan'
));
$content=$view->render($viewModel);

$viewLayout = new \Zend\View\Model\ViewModel();
$viewLayout->setTemplate('wmsLayout')->setVariables(array(
        'content' => $content,
        'anyOtherVariableForLayout'=>'layout variable value',
    ));
echo $view->render($viewLayout);

希望有人帮助我。