Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blade: escaping text and allowing new lines

I have a user-input text displayed on one of the pages. I want to allow new lines, though. How do I display the text, so it is escaped AND allows new lines?

I used nl2br() and Blade's tripple brackets {{{$text}}}, however, obviously, the tripple brackets escape <br/> tags as well.

Is there a way to combine escaping and HTML's new lines using Blade?

Thanks.

like image 462
lesssugar Avatar asked Jan 19 '15 14:01

lesssugar


2 Answers

You can do the escaping first, using e() and then apply nl2br():

{{ nl2br(e($text)) }} 

e() is the function Blade uses when compiling triple brackets

like image 53
lukasgeiter Avatar answered Oct 09 '22 16:10

lukasgeiter


You can use this

{!! nl2br(e($text)) !!} 
like image 26
Salar Avatar answered Oct 09 '22 16:10

Salar