Laravel Blade – Show new line in html

June 24, 2022
Laravel
Laravel Blade – Show new line in html

In this post you will learn How to show new line in HTML for laravel blade.

Let’s get started.

Problem

This of the common problem where you add line breaks in HTML and when you display in html you get all text in one line.

That is because new line (\n) which you typed in the WYSIWYG editor or textarea. It’s not understandable nor renderable by the HTML.

Solution

Thanks to the PHP which has a function called nl2br() which we can use in laravel blade as well.

{!! nl2br(e($data)) !!}
// OR
{ nl2br(e($data)) }}
  • The nl2br() function converts the new line (\n) to br html tag
  • The e is helper function of laravel that runs htmlentities over the given string.

Example

{!! nl2br(e("One line.\nNew Line")) !!}

The browser output of the above code will be:

One line.
New line.

The HTML Output of the above code will be

One line.<br>
New line.

Conclusion

Hope you have learned today in the above article about how to show a new line in HTML for laravel blade. See you the next one 🙂

Write a Reply or Comment

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


Icon