Keystone / Nunjucks,数据不会传递到模板

时间:2019-07-25 06:47:19

标签: javascript node.js templates keystonejs nunjucks

我正在使用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 %}

1 个答案:

答案 0 :(得分: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);

}