简体   繁体   中英

Keystone/Nunjucks , data does not pass to template

I am using Keystone JS and nunjucks. I have a feature where in the application sends an email. There is no problem in sending the email , the problem is that the data does not pass to the template. It does not adapt.

Code

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);


    }

Template

<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 %}

Because according to the doc , if you need to send locals you need to send it inside the .send() method:

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

So in your case it will be:

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM