Search code examples
node.jspugemail-templates

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.


Solution

  • 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