简体   繁体   English

Node.js-Pug模板-动态呈现href

[英]Node.js - Pug Template - Render href dynamically

I am having a API written in Node.js with Express.js and Mongoose. 我有一个用Express.js和Mongoose用Node.js编写的API。 For the mailers, I use email-templates npm module. 对于邮件,我使用email-templates npm模块。 I use the PUG templating engine to present my email. 我使用PUG模板引擎显示我的电子邮件。

I pass some variables to the template and want to use that as href inside my emails. 我将一些变量传递给模板,并希望在电子邮件中将其用作href。

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. 此处,数据是从节点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. 但是,正确接收到#{name}并将其显示在电子邮件中。

The pug team removed the support for interpolation in attributes in Pug v2. Pug团队在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. 或者,如果您的环境支持,则使用ES2015 模板字符串功能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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