【问题标题】:How to send res.render string parameter and render it as HTML?如何发送 res.render 字符串参数并将其呈现为 HTML?
【发布时间】:2019-08-06 21:45:47
【问题描述】:

我正在尝试将 html 字符串作为 res.render() 参数发送并在客户端将其呈现为 html。如何让浏览器将其识别为 html 而不仅仅是字符串?这个可以吗?

home.hbs

<div id="results">
  {{#each results}}
    <p>{{this}}</p>
  {{/each}}
</div>

server.js

app.get('\', (req,res) => {
  let results = ['<mark>This</mark> is highlighted.']
  res.render('home', {results: results});
})

在我的网页中,我希望它看起来像一个段落,其中“This”一词被突出显示(因为&lt;mark&gt;),但它显示为&lt;mark&gt;This&lt;/mark&gt; is highlighted.,其中This 没有突出显示并且标记标签是纯文本。

【问题讨论】:

  • 你可以通过使用 ejs 模板来做到这一点:ejs.co

标签: javascript html node.js express


【解决方案1】:

使用“triple-stash”取消 html 转义功能。
https://handlebars-lang.github.io/docs/guide/#html-escaping

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2020-06-11
    • 2022-08-24
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多