【问题标题】:Keystone/Nunjucks , data does not pass to templateKeystone/Nunjucks ,数据不传递给模板
【发布时间】:2019-07-25 14:50:58
【问题描述】:

我正在使用 Keystone JS 和 nunjucks。我有一个功能,可以在应用程序中发送电子邮件。发送邮件没有问题,问题是数据没有传递到模板。它不适应。

代码

var sendEmail = function (err, results) {
        if (err) return callback(err);
        async.each(results.admins, function (admin, done) {
            new keystone.Email({ templateName: 'enquiry-notification.html', transport: 'mailgun', engine: cons.nunjucks, root: 'templates/emails' }).send({
            }, {

                    apiKey: '',
                    domain: '',
                    title: "Test",
                    author: 'test',
                    body: 'Heeeeeeeeeeeeeeeeeeeeeeeeeeeee',
                    subject: subject,
                    html: '<b>NodeJS Email Tutorial</b>',
                    body: "Helloworld",
                    to: admin.email,
                    text: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaavbbb',
                    host: 'helloworld.com',
                    from: {
                        name: 'Test Mail',
                        email: inquiry.email
                    },
                    inquiry: inquiry,
                    brand: brand,
                }, done);
        }, callback);


    }

模板

<h1>Hi %recipient%</h1>
<p class="text-larger">An enquiry was just submitted to %from.name%:</p>

{% if inquiry.email %}
    <p class="text-larger">From
    {% if inquiry.name.full %}
        <strong>{{ inquiry.name.full }}</strong>
    {% endif %}

{% endif %}

【问题讨论】:

    标签: javascript node.js templates keystonejs nunjucks


    【解决方案1】:

    因为根据doc,如果需要发送本地人需要在.send()方法里面发送:

    new Email(/* ... */).send({
        recipient: {
            firstName: 'Max',
            name: 'Stoiber',
        }
    }, {/* ... */}, function (err, result) {/* ... */});
    

    所以你的情况是:

    const sendEmail = function (err, results) {
      if (err) return callback(err);
      async.each(results.admins, function (admin, done) {
        new keystone.Email({ templateName: 'enquiry-notification.html', transport: 'mailgun', engine: cons.nunjucks, root: 'templates/emails' })
        .send({
          inquiry: inquiry,
          brand: brand
        }, { /*....*/ }, done);
      }, callback);
    
    }
    

    【讨论】:

    • 啊,好吧,我明白了,另外,我们可以发送消息正文吗?不使用模板?喜欢主题,因为我确实尝试使用正文,文本但它不起作用
    • 因为我想发送 ADF XML
    • 制作模板只需{{ bodyContent }},然后在里面发送即可,.send({bodyContent:'.....'});
    • 但最好提出不同的问题并关闭这个问题,因为这个答案解决了当前的问题。
    • 不,我不是,这就是为什么我说把它放在另一个问题上:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2014-05-20
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多