Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJS - convert new lines to <br/> and keep the content encoded

Tags:

node.js

ejs

This is similar to Jade - convert new lines to <br/> and keep the content encoded

so is there easy way to do the same in ejs:

<%- escape(foo).replace(/\n/g, '<br/>') %>

This could be possible by extracting escape method from ejs and assign to res.locals, but I'm looking for something more elegant if it's possible

like image 351
Brankodd Avatar asked Nov 09 '25 00:11

Brankodd


1 Answers

I had a similar problem, when I had to display git commit message in my page. I had a look at how GitLab formats commit messages and found out that they use white-space: pre-wrap. https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

So in my .ejs file I use:

<div style="white-space: pre-wrap"><%= commit.body %></div>
like image 197
Marcin Avatar answered Nov 10 '25 21:11

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!