由于我有重復的代碼,有沒有更好的方法來做到這一點?
我有一個帶有鏈接的電子郵件樹枝,可以在瀏覽器中打開電子郵件。
目前我不得不渲染模板兩次。
一次獲取內容并將其保存到 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 %}
uj5u.com熱心網友回復:
IMO,我認為您可能會使事情變得過于復雜而無濟于事。
兩次渲染模板不會對性能造成太大影響,無論如何都會快取這些模板。我想主要是因為代碼“更整潔”而沒有不必要的重復,這很好,但不是一個重要的問題。
但是如果你想避免render()為同一個模板呼叫兩次......
只需有一個不包含“在瀏覽器上讀取”位的模板,并僅渲染它。
對于電子郵件版本,請添加“在瀏覽器上閱讀”鏈接。
$htmlBody = $this->twig->render('EmailRo/incomplete-listing-moderation/accept-incomplete-listing.email.twig', [
'user' => $admin,
'avatar' => AmazonS3Service::URL_PREFIX.$admin->getPhoto(),
]);
// all the S3 logic, which returns an URL
$emailUrl = storeEmailHtmlSomewhere($emailBody);
$emailBody = <<< HTML
<a href="$emailUrl" 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>
HTML;
// do what you need with that email
sendEmail($emailHeader . $emailBody);
你可以簡單地將這兩個部分存盤在不同的模板中,如果涉及到它,并在發送電子郵件時連接結果。
$emailHtmlBody = $this->twig->render('EmailRo/incomplete-listing-moderation/accept-incomplete-listing.email.twig', [
'user' => $admin,
'avatar' => AmazonS3Service::URL_PREFIX.$admin->getPhoto(),
]);
// all the S3 logic, which returns an URL
$emailUrl = storeEmailHtmlSomewhere($emailBody);
$emailHtmlHead = $this->twig->render('EmailRo/header-with-link.email.twig', [
'emailUrl' => $emailUrl
]);
sendEmail($emailHtmlHead . $emailHtmlBody);
uj5u.com熱心網友回復:
是這樣的:你可以使用3個模板,而不是一個base_mail.html.twig像與主郵件內容,以及兩個薄包裝email_s3.html.twig,并email.html.twig在那里你給基郵件原始HTML作為引數。
這將在控制器中給出類似的內容(未測驗,抱歉打字錯誤):
$emailBase = $this->twig->render('base_mail.html.twig', [
'user' => $admin,
'avatar' => AmazonS3Service::URL_PREFIX.$admin->getPhoto(),
]);
$emailBodyS3 = $this->twig->render('email_s3.html.twig', [
'base' => $emailBase,
's3html' => /* ... */,
]);
// Handle $emailBodyS3 with S3
$emailBody = $this->twig->render('email.html.twig', [
'base' => $emailBase,
]);
// Send the email with $emailBody
薄包裝看起來像這樣:
電子郵件.html.twig
<div>{{ base | raw }}</div>
email_s3.html.twig
<div>
{{ base | raw }}
<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>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/331938.html
上一篇:集合型別新增元素關系null
