How do I use nl2br() in Laravel 5 Blade
Asked Answered
H

5

40

So I want to keep linebreaks from the database while using the Blade Template Engine. I came up on the idea using

{!! nl2br(e($task->text)) !!}

It works. But it looks like a needlessly complicated solution. Is there a better way?

Hoxie answered 17/2, 2015 at 19:51 Comment(2)
why e() and what is that function? I can use {!! nl2br($var) !!} nothing more needed.Rhodes
@AmitShah You really should use it. Especially when printing user's input. Have a look at #33075658Hoxie
C
35

You can define your own "echo format" that will be used with the regular content tags {{ ... }}. The default format is e(%s) (sprintf is used to apply the formatting)

To change that format call setEchoFormat() inside a service provider:

public function boot(){
    \Blade::setEchoFormat('nl2br(e(%s))');
}

Now you can just use the normal echo tags:

{{ $task->text }}

For echos you don't want nl2br() applied, use the triple brackets {{{ ... }}}


To switch the function of the brackets (triple and double) around, do this:

\Blade::setContentTags('{{{', '}}}');
\Blade::setEscapedContentTags('{{', '}}');
Conjuration answered 17/2, 2015 at 19:59 Comment(3)
Awesome! Is there also a way to swap it? Using only triple brackets for the nl2br?Hoxie
related answered question which was helpful to me: https://mcmap.net/q/325229/-blade-escaping-text-and-allowing-new-linesCutthroat
Does not work as of L5.4, use: {!! nl2br(e($text)) !!}, for more, see: github.com/laravel/framework/issues/17736;Camboose
J
26

Simple approach which works for Laravel 4 + Laravel 5.

{!! nl2br(e($task->text)) !!}
Jacey answered 25/3, 2019 at 18:1 Comment(3)
This is exactly what is was written in the question :DHoxie
Yep and its still the codeless's solution for it. =) Less code than in the accepted answer here. Also the accepted answer does not work for Laravel5.Jacey
It works :) Thanks!!Compeer
A
6

Below solution worked in blade file in Laravel 5.7 version for me:

{!! nl2br(e($contactusenquiry_message), false) !!}

Thanks to ask this question.

Amitie answered 30/9, 2020 at 18:54 Comment(0)
A
4

This is a way to do it while keeping everything safe.

Works on all Laravel versions: Laravel v4 - Laravel v10.

<?php foreach (explode("\n", $text) as $line) { ?>
 {{$line}}<br />
<?php } ?>
Airspace answered 23/10, 2021 at 9:10 Comment(0)
P
0

A slightly cleaner alternative if you're using Eloquent is Mutators. On your Task model create a method like this:

public function getTextAttribute($value)
{
    return nl2br(e($value), false);
}

Now you can use {!! $task->text !!} and it will output the HTML correctly and securely. The good thing about this method is you can do all manner of conversions in the get...Attribute method, such as adding wrapper tags or using Markdown.

If you need access to both the raw data and HTML version you could replace the above with this:

public function getTextHtmlAttribute()
{
    return nl2br(e($this->text), false);
}

Then you would use {{ $task->text }} for the original and {!! $task->text_html !!} for the HTML version.

Pumice answered 16/8, 2019 at 10:46 Comment(3)
Should work, but you need to repeat this for every model. But I think there are usages for this approach.Hoxie
@jascha Yes that's true, but you usually only want this for certain fields, not everything.Pumice
Not cleaner it all. If you look at {!! $task->text !!} you would have no way of telling something is going on under the hood. In essence it is doing exactly the opposite of what you would expect seeing this code in the view.Airspace

© 2022 - 2024 — McMap. All rights reserved.