【发布时间】:2021-12-08 21:43:24
【问题描述】:
由于我有重复的代码,有没有更好的方法来做到这一点?
我有一个电子邮件分支,其中包含在浏览器中打开电子邮件的链接。
目前我必须渲染模板两次。
一次获取内容并将其保存到 S3 中的 html 文件。第二次在发送的邮件中添加S3链接,方便在线查看邮件。
$emailBody = $this->twig->render('EmailRo/incomplete-listing-moderation/accept-incomplete-listing.email.twig', [
'user' => $admin,
'avatar' => AmazonS3Service::URL_PREFIX.$admin->getPhoto(),
's3html' => '',
]);
$s3 = $this->container->get('s3storage');
$fileName = rand(1000, 999999) . time() . '.html';
file_put_contents($fileName, $emailBody);
$file = $this->container->get('request_stack')->getCurrentRequest()->server->get('DOCUMENT_ROOT').'/'.$fileName;
$s3->upload('users/' . $fileName,
file_get_contents($file),
mime_content_type($file));
$s3html = AmazonS3Service::URL_PREFIX . 'emails/' . $fileName;
$emailBody = $this->twig->render('EmailRo/incomplete-listing-moderation/accept-incomplete-listing.email.twig', [
'user' => $admin,
'avatar' => AmazonS3Service::URL_PREFIX.$admin->getPhoto(),
's3html' => $s3html,
]);
在树枝上我是这样渲染的
{% if s3html %}
<a href="{{ s3html }}" style="text-decoration: none;"><span style="font-family:'Montserrat','Arial','Helvetica', sans-serif !important; font-weight: normal; font-size:13px; line-height: 15px; color: #27AAE1; font-weight: 400;">
Email not displayed correctly? Read the online version in your browser.
</span></a>
{% endif %}
【问题讨论】:
-
为什么不添加一个占位符,例如模板中的
{s3html}并使用str_replace更改呈现的html?这样你只需要渲染一次并做两次str_replaces