【问题标题】:How to set dynamic style properties in EJS?如何在 EJS 中设置动态样式属性?
【发布时间】:2021-05-02 10:18:12
【问题描述】:

我正在使用 EJS 渲染引擎从 Node.js 发送邮件。我需要动态设置背景颜色、文字颜色等属性。

类似style ="background: <%=primary_color%>"

或者如果我们可以动态选择类

class="<%=primary_class%>"

【问题讨论】:

  • 这个有什么答案吗?

标签: html css node.js ejs


【解决方案1】:

您可以从 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>

【讨论】:

    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多