【发布时间】:2021-05-02 10:18:12
【问题描述】:
我正在使用 EJS 渲染引擎从 Node.js 发送邮件。我需要动态设置背景颜色、文字颜色等属性。
类似style ="background: <%=primary_color%>"
或者如果我们可以动态选择类
class="<%=primary_class%>"
【问题讨论】:
-
这个有什么答案吗?
我正在使用 EJS 渲染引擎从 Node.js 发送邮件。我需要动态设置背景颜色、文字颜色等属性。
类似style ="background: <%=primary_color%>"
或者如果我们可以动态选择类
class="<%=primary_class%>"
【问题讨论】:
您可以从 node.js express 服务器和ejs.renderFile 传递背景颜色。
//server.js
let bgColors = {
primary: '#000',
accent: '#fff',
gray: '#eee'
}
app.get('/', (req, res) => {
ejs.renderFile('home.ejs', {bgColors}, {}, (err, template) => {
if (err) throw err
res.end(template)
})
})
然后在home html文件中就可以使用bgColors的属性名了。
<div style="background: <%= bgColors.primary %>">Some text</div>
<div style="background: <%= bgColors.accent %>">Some more text</div>
<div style="background: <%= bgColors.gray %>">Some more text</div>
【讨论】: