在PUG中解析数据库中的数据 [英] Parsing data from database in PUG

查看:40
本文介绍了在PUG中解析数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mongo数据库中有此数据,但我无法控制它,因为从应用程序导出到数据库的数据会更新html标记(我认为正确的术语是‘编码’,但我不确定)

<div>This is some test profile text<br /></div><div><br /></div><div>Hop this prints fine<br /></div><div><br /></div><div><b>Hello</b><br /></div><div><br /></div><div>Testing</div>

当我尝试用

解析它时
      .exhibitor-profile
          | !{exhibitor.profile}

打印如下

<div>This is some test profile text<br /></div><div><br /></div><div>Hop this prints fine<br /></div><div><br /></div><div><b>Hello</b><br /></div><div><br /></div><div>Testing</div>

如果我使用

      .exhibitor-profile
          | #{exhibitor.profile}

打印如下

&lt;div>This is some test profile text&lt;br />&lt;/div>&lt;div>&lt;br />&lt;/div>&lt;div>Hop this prints fine&lt;br />&lt;/div>&lt;div>&lt;br />&lt;/div>&lt;div>&lt;b>Hello&lt;/b>&lt;br />&lt;/div>&lt;div>&lt;br />&lt;/div>&lt;div>Testing&lt;/div>

与数据库中的数据基本相同。但是我希望它输出为HTML..

如何执行此操作?

推荐答案

如果您正在使用节点,请继续阅读。

安装js-htmlencode包:

npm install -S js-htmlencode
然后通过htmlDecode方法运行一次原始数据库输出。在将数据传递到PUG脚本之前,您应该在服务器应用程序中执行此操作:

服务器Javascript:

const htmlDecode = require("js-htmlencode").htmlDecode;
app.get("/htmldecode", (req, res) => {
  const raw = "&lt;h1>This is &lt;span style='color:red'>RED&lt;/span>!!&lt;/h1>"
  res.render("htmldecode", { raw: raw, decoded: htmlDecode(raw) })
});

htmldecde.pug:

html
  head
  body 
    h3 Html Decoding Twice
    p Using !: !{raw}
    p Using #: #{raw}
    p Final: !{decoded}

实际产量:

需要注意的是,!{raw}不会渲染到<h1>…中。它按字面表示,即转换为&lt;h1>…。该浏览器将&lt;显示为<

请注意使用!运算符时的所有注意事项。

这篇关于在PUG中解析数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆