将树枝渲染到 html 文件,避免重复代码添加变量
Rendering twig to html file, avoid duplicate code to add a variable
我有重复的代码,有更好的方法吗?
我有一个带有 link 的电子邮件分支,用于在浏览器中打开电子邮件。
目前我必须渲染模板两次。
一次获取内容并将其保存到S3中的html文件中。第二次将S3link添加到已发邮件,在线查看邮件
$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 %}
您可以使用 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
薄包装纸看起来像这样:
email.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>
IMO,我认为您可能将事情过于复杂化而没有什么好处。
两次渲染模板不会对性能造成太大影响,无论如何都会进行缓存。我想主要是担心代码是“整洁的”,没有不必要的重复,这很好,但不是一个重要的问题。
但是如果您想避免为同一模板调用 render()
两次...
只需要一个不包含“在浏览器上读取”位的模板,并只渲染它。
对于电子邮件版本,请在前面加上“在浏览器上阅读”link。
$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);
我有重复的代码,有更好的方法吗?
我有一个带有 link 的电子邮件分支,用于在浏览器中打开电子邮件。
目前我必须渲染模板两次。
一次获取内容并将其保存到S3中的html文件中。第二次将S3link添加到已发邮件,在线查看邮件
$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 %}
您可以使用 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
薄包装纸看起来像这样:
email.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>
IMO,我认为您可能将事情过于复杂化而没有什么好处。
两次渲染模板不会对性能造成太大影响,无论如何都会进行缓存。我想主要是担心代码是“整洁的”,没有不必要的重复,这很好,但不是一个重要的问题。
但是如果您想避免为同一模板调用 render()
两次...
只需要一个不包含“在浏览器上读取”位的模板,并只渲染它。
对于电子邮件版本,请在前面加上“在浏览器上阅读”link。
$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);