【问题标题】:How to render a mail template with layout in ZF2?如何在 ZF2 中渲染带有布局的邮件模板?
【发布时间】:2012-12-31 19:15:40
【问题描述】:

在 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;
}

【问题讨论】:

  • 出于什么原因需要布局?通常,使用 view 呈现电子邮件正文就足够了。布局(因此一个视图封装了另一个视图)对于电子邮件来说太过分了。
  • @Jurian 我认为,该布局是存储页眉和页脚的好地方,例如徽标和免责声明,每封电子邮件都相同。

标签: php email templates layout zend-framework2


【解决方案1】:

谢谢!你的回答帮助了我!

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

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

【讨论】:

    【解决方案2】:

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

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

    查看/email/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>
    

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

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

    【讨论】:

      【解决方案3】:

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

      是中文写的,不过我觉得只要看代码就够清楚了。你也可以read it with help of Google translation

      【讨论】:

      • 中文链接加1!
      【解决方案4】:

      对于 sendig 电子邮件,我推荐以下模块:https://github.com/WasabiLib/Mail

      模块基于 ZF2 消息,并被配置为服务。所以你只需要调用servicemanager。

      body 方法能够处理 ZF2 视图模型。请看下面的例子:

      $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 即可。

      【讨论】:

        【解决方案5】:

        @user1986560 回答不完整,因为您必须需要一些基本的帮助程序,例如带有路由器的“url”,有时还需要“basePath”。

        这是完整的代码-

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

        希望它对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-13
          • 1970-01-01
          • 1970-01-01
          • 2011-04-28
          • 2012-10-17
          相关资源
          最近更新 更多