Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

pug

I am still not that familiar with the Jade template engine. Is there a way to convert the new lines such as \n to br tags and at the same time keep the other content encoded?

For example

.replace(/\n/g,'</br>')

applied over the encoded value should do the work. However I am not sure how to encode the value and get the result. Is there any helper for this?

like image 851
Petur Subev Avatar asked Jan 26 '13 19:01

Petur Subev


2 Answers

if you don't exactly need <br>, you can simply put your text within <pre> tag. All whitespaces (including new lines) will be displayed as typed.

Or you could set css rule white-space: pre to preserve whitespace preformatting. Check MDN on this.

like image 111
eagor Avatar answered Oct 15 '22 20:10

eagor


You can use jades escape method and replace the linebreaks in the return value of that like so:

p !{escape(foo).replace(/\n/g, '<br/>')}

I am not aware of any built-in functionality for your use case.


Looks like pug got rid of the escape function, so this is what you would have to use now:

p !{foo.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br/>')}
like image 31
dave Avatar answered Oct 15 '22 20:10

dave