[使用Mustache.js传递参数

问题描述 投票:0回答:1

例如:我想向电子邮件验证失败的用户发送电子邮件。

我尝试过的...

authCtrl.js

  const template = require('../templates')
  let templateName = (success) ? 'templateEmailValidationConfirmation' : 
  'templateEmailValidationFailure';
  let template = templates.getTemplate(templateName);
  return emailHelper.compileMailContent(template.template);

emailHelper.js

  const mustache = require('mustache');
  const compileMailContent = (template) => {

     let params ={
      recipientName : 'Ajay'
     }

     const compileFunc = mustache.render(template, params);
     console.log(compileFunc);
     return compileFunc;
};

templates.js

 const templateEmailValidationFailure =

   '<!DOCTYPE html>\n' +
   '\n' +
   '<head>\n' +
   '    <title>Email Not verified</title>\n' +
   '</head>\n' +

   '  <div>\n' +
   '    <h4 class="font-weight-bold text-danger">\n' +
   '      </i>  Sorry {{recipientName}} ! Your mail could not be verified</h4>\n' +
   '      <hr/>\n' +
   '      <div>\n' +
   '        This could be because either your verification code is incorrect or the URL has expired 
            or has been mistyped.\n' +
   '      </div>\n' +
   '  </div>\n' +
   '</div>';

 const templates = [
    {
    id: 'templateEmailValidationFailure',
    template: templateEmailValidationFailure
    },
 ]
 exports.getTemplate = (id) => {
 return templates.find((obj) => {
 return obj.id === id;
 });

};

这使我从template.js中抛出错误-无法读取'recipientName'的属性。根据{C},HTML中使用的DOCUMENT参数显示在{{}}中。我的代码有什么问题?

node.js mustache
1个回答
0
投票

这段代码:

Mustache.js

没有道理。您正在尝试创建一个对象,所以可以这样做:

let params = {
  recipientName = 'Ajay'
}
© www.soinside.com 2019 - 2024. All rights reserved.