[英]show html email template element only when variable value is not null/undefined/empty
我在 email 中使用 mailgun,並在 node.js 中使用 html 作為模板。 我只想在變量有效時顯示 html 標簽。
下面是我的 function 我正在替換變量值的地方。
exports.sendParentReservation = async function(data) {
let template = await loadTemplate(parentReservation);
let embeddedLogo = await loadLogo();
// > PARENT DATA: parent name, kid name, service name, company name, group name, reservation date,price
template = template.replace(/{{action_url}}/g, parentReservationsLink);
template = template.replace(/{{parent_name}}/g, data.parentName);
template = template.replace(/{{nextStep}}/g, data.nextStep);
mailer.sendMail({
from: '...',
to: data.recipient,
subject: "Your reservation on ...",
html: template,
attachments: [{
cid: 'logo.png',
content: embeddedLogo,
encoding: 'base64'
}],
}, (err, info) => {
if (err) {
console.log(`Error: ${err}`);
}
else {
console.log("Mailgun Response: " + JSON.stringify(info,null,4));
}
});
};
下面是我的 html email 模板代碼
<table class="attributes" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="attributes_content">
<table width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="attributes_item"><span class="f-fallback"><strong>Instructions - Next/Steps</strong></span></td>
</tr>
<tr>
<td class="attributes_item">{{nextStep}}</td>
</tr>
</table>
</td>
</tr>
</table>
如果nextStep
為null/undefined or empty
,我想隱藏 html 代碼上方的所有這些。
你應該能夠做到這一點:
<script>
if (nextStep != null) {
document.write('<table class="attributes" width="100%" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td class="attributes_content"> <table width="100%" cellpadding="0" cellspacing="0" role="presentation"> <tr> <td class="attributes_item"><span class="f-fallback"><strong>Instructions - Next/Steps</strong></span></td> </tr> <tr> <td class="attributes_item">{{nextStep}}</td> </tr> </table> </td> </tr> </table>')
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.