html - 如何在 EJS 中设置动态样式属性?
问题描述
我正在使用 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>
推荐阅读
- python-3.x - 在python中定义矩阵时放置常量
- xml - 如何以结构化的方式将表格结构转换为纯文本
- c++ - 使用 async cpp boost socket 时减少响应时间
- mongodb - 使用 MongoDb 聚合管道根据特定类型查询前 10 项
- python - 如何有效地读取非常大的 gzip 压缩日志文件的最后一行?
- sql - 在 T-SQL 中为表创建或更改
- sql - 如何在 Presto SQL 中应用 sum() 查询并面对函数 sum 的意外参数 (varchar)
- python - 查找列表中包含重复值的每个元素的索引
- python - “符号”和“标记”在 plotly-express 的 line 函数中似乎不再起作用
- php - Google drive API - 获取已创建权限的用户(共享文件)