Search code examples
node.jsejshtml-entities

NodeJS converts passed html content to entities by default


In my website I want to have a tree of categories. To get this to work, I need to arrange them in a parent-child tree through a recursive loop. What I did was to fetch the category data from the database, run the loop and generate the output HTML content inside of the NodeJS code. Then I pass the generated HTML string to my EJS document to be shown in the browser.

Now here is the problem: the generated string is just how it should be as long as it is inside the NodeJS function. But the string that is passed on has some characters converted into identities, and I couldn't find a way to convert them back. For eg:

<h1>Hello world</h1>

becomes

&lt;h1&gt;Hello world&lt;/h1&gt;

inside the ejs document, but when I try to print the string using console.log inside of some js file, it prints the correct HTML string instead of the converted one.


Solution

  • You should use <%- as the opening tag for this as it outputs the unescaped value into the page.

    See the 'Tags' section in the EJS docs for more reference

    https://ejs.co/#docs