forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

can't seem to pass variables to pug template #235

Closed ryanpag3 closed 7 years ago

ryanpag3 commented 7 years ago

I'm trying to render a pug/jade template with node email templates.

                var confirmCode = generateConfirmCode(configPublic.confirmCodeLength);
                var query = querystring.stringify({
                    code: confirmCode,
                    id: user._id.toString()
                });

                var templateDir = path.join(__dirname, '../templates', 'confirmation-email');
                var confirmEmail = new EmailTemplate(templateDir);
                var confirmUrl = configPublic.url + '/user/email/confirm?' + query;
                var templateVals = {url: confirmUrl};

                confirmEmail.render(templateVals, function(err, result) {
                    if (err) {
                        console.log(err);
                    }
                    var mailOptions = {
                        from: configPrivate.gmail.username,
                        to: user.email.address,
                        subject: 'confirmation',
                        html: result.html
                    };
                    email.send(mailOptions)
                        .then(function (successMsg) {
                            db.setConfirmCode(user, confirmCode)
                                .catch(function (err) { // catch setConfirmCode err
                                    deferred.reject(err);
                                });
                            deferred.resolve(successMsg);
                        })
                        .catch(function (err) { // catch send err
                            deferred.reject(err);
                        })

So I pass templateVals to the render call, and I'm trying to figure out how to display that information on the rendered template like so:

link(rel='stylesheet', href='./style.css')
|
|
p !{url}
p #{url}
p url
p templateVals.url
p #{templateVals.url}
p !{templateVals.url}

and none of these combinations seem to be working. I'm not sure what I'm doing wrong here. Thanks for your help!

ryanpag3 commented 7 years ago

ended up being an issue with my webstorm file watcher messing up the template directory and adding an .html file causing the render to fail.

niftylettuce commented 7 years ago

@ryanpage42 glad to hear- please watch this repo and follow me on twitter if you're interested in the v3 release of this package, a complete rewrite

newkillerbeast2017 commented 6 years ago

@niftylettuce just a quick question. Have something like this:

locals: {
    name: obj.first_name + " " + obj.last_name,
    token: obj.token
}

and in the template:

a.button(href='http://xyz.com/verify/#{token}', target='_blank') Activate your account

It says invalid token at #. How do I solve this dynamically?

niftylettuce commented 6 years ago
-a.button(href='http://xyz.com/verify/#{token}', target='_blank') Activate your account
+a.button(href=`http://xyz.com/verify/${token}`, target='_blank') Activate your account

Also try putting this above the a.button line in your template and see what it outputs:

- console.log('token', token)

It seems token is undefined?

And see what it outputs in console

newkillerbeast2017 commented 6 years ago

Got it working with your change itself. Thanks a ton!!