Skip to content Skip to sidebar Skip to footer

Parsing Data From Database In Pug

I have this data in my mongo database which I can't control as data exported from my application to the database updates the html tags (I think the right term is 'encoding' but I a

Solution 1:

If you are using Node read on.

Install the js-htmlencode package:

npm install -S js-htmlencode

Then run your raw database output through the htmlDecode method once. You should do it in your server app before passing the data to your Pug script:

Server Javascript:

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

htmldecode.pug:

html
  head
  bodyh3Html Decoding Twice
    p Using !: !{raw}
    p Using #: #{raw}
    p Final: !{decoded}

Actual output:

enter image description here

It should be noted that !{raw} does not render into <h1>…. It renders literally, ie to &lt;h1>…. It is the browser that shows &lt; as <.

Do take note of all the cautions that come with using the ! operator.

Post a Comment for "Parsing Data From Database In Pug"