laravel blade shows a new line HTML

The problem of laravel blade shows a new line HTML

This problem of laravel blade shows a new line HTML relates to the functionality of line breaks in PHP. A scenario for the same is when a programmer is trying to enter the text in the database the text appears in the form of a line break. The cause occurs when one tries to show all the same text in the blade.php view in which all the text appears without a line break. 

One can try to use functions like nl2br() or str_replace to solve this issue but this will only result in changing the /n tag to the br tag. This will cause a writing line break next to the text. 

Solution

One can find the solution to the above problem in PHP itself. The function named nl2br() is useful for the same purpose. It is also available for laravel blade as well. The code for the same is:

 

{!! nl2br(e($data)) !!}
// OR
{ nl2br(e($data)) }}

 

Another option is to use the escaping before applying the above function. The code is:

 

{{ nl2br(e($user->proffile)) }}

 

This function is very common in blades while dealing with the triple brackets. 

The new line (n) is converted to the br html tag via the nl2br() method. The e is a Laravel utility function that executes htmlentities on the supplied string.

One can also use !! !! sign instead of brackets {{ }}. 

 

{!! nl2br($user->proffile) !!}

You can use nl2br(e($user->proffile)) in Laravel 5, but the browser will display html instead. When done in this manner, the HTML!! nl2br($user->proffile)!! is not displayed.

 

 

Also Read: What is IndexError and how to solve it?

 

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *