Miha Jamsek Miha Jamsek - 1 year ago 79
Javascript Question

Display line break from mysql text type to ejs

I have MySQL table with column PROJ_ABOUT which is type TEXT.

I inserted multiple rows into this column and now I am trying to diplay this entry in my Express.js app using ejs engine.

<h2>About project</h2>
<p><%= rows.PROJ_ABOUT %></p>

However, when the text is inserted into ejs file, all line breaks are lost.

**Instead of this:**
Line 1
Line 2
**I get this:**

I tried getting around this by saving entry to database with
<br />
instead of
but instead of getting line breaks I got text with br tags written as common text.

**So i got this:**
Line1<br />Line2

Now, I saw the answers for PHP, but i am using Node.js with ejs templating engine and I am looking for a solution using JS.

Answer Source

You need to use the proper EJS syntax for rendering unescaped HTML, otherwise you will get the escaped strings.

Short answer would be :

<!--    V You should use - instead of = -->
<p>   <%- rows.PROJ_ABOUT %></p> 

this will render your rows.PROJ_ABOUT variable as unescaped HTML

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download