Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - Pug Template - Render href dynamically

I am having a API written in Node.js with Express.js and Mongoose. For the mailers, I use email-templates npm module. I use the PUG templating engine to present my email.

I pass some variables to the template and want to use that as href inside my emails.

Here is the code I am struggling with:

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

Here the data is being passed to the template from the node API.

and in the template:

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

It says invalid token at #. How do I solve this dynamically? However the #{name} is properly received and shown in the email.

like image 449
Killer Beast Avatar asked Jan 27 '26 15:01

Killer Beast


1 Answers

The pug team removed the support for interpolation in attributes in Pug v2. You have to use another syntax for it.

You can either compose your string:

a.button(href='xyz.com/verify/' + token, target='_blank') Activate your account

Or else use ES2015 template strings feature if your environment support it.

a.button(href=`xyz.com/verify/${token}`, target='_blank') Activate your account
like image 177
tibuurcio Avatar answered Jan 29 '26 04:01

tibuurcio