Warm tip: This article is reproduced from serverfault.com, please click

node.js-邮戳模板:动态变量,将html处理为文本而不是html

(node.js - Postmark template: dynamic variable with html processed as text instead of html)

发布于 2020-11-28 21:04:02

我有一个邮戳电子邮件模板,其中动态变量包含html。但是,它将html处理为纯文本。

更具体地说,我有下面的代码。该变量body将发送到电子邮件模板,但是该变量<br><br>显示为文本,而不是转到下一行。

在控制器中:

const body = "Hi...<br><br> Welcome to our a new episode.";
client.sendEmailWithTemplate(
    {
        TemplateAlias: process.env.POSTMARK_TEMPLATE,
        TemplateModel: {
            body: body
        },
        From: from,
        To: email,
    });

在邮戳模板中:

<tr>
  <td>
    1. The following includes text with a br break: {{body}}
  </td>
</tr>
<tr>
  <td>
    2. This line includes a br break directly in the template: How are you? <br> Anything new?
  </td>
</tr>

这将导致在电子邮件中br中断确实在位置2上起作用,但在位置1上不起作用。在位置1<br> <br>上显示为文本“ <br> <br>”。

我究竟做错了什么?

Questioner
Nick
Viewed
11
Nick 2020-11-30 20:22:59

邮戳支持提供了答案:

我们通常会转义HTML,但是你可以通过对变量使用以下语法来解决此问题:

{{{body}}}或{{&body}}

三元括号或“&”符号可让你在填充TemplateModel值时将HTML放入该变量中。