Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying HTML with Blade shows the HTML code

I have a string returned to one of my views, like this:

$text = '<p><strong>Lorem</strong> ipsum dolor <img src="images/test.jpg"></p>' 

I'm trying to display it with Blade:

{{$text}} 

However, the output is a raw string instead of rendered HTML. How do I display HTML with Blade in Laravel?

PS. PHP echo() displays the HTML correctly.

like image 945
lesssugar Avatar asked Mar 25 '15 11:03

lesssugar


People also ask

How do I show HTML code in blade?

Display HTML In Laravel Blade Views By default you would use the following syntax {{ $some_variable }} to echo out the content of a specific variable in Blade. By default the {{ }} escapes the HTML tags. In the first case, you can see how the HTML elements were escaped, and in the second case where we use {!! !!}

How to show HTML code in Laravel Blade?

You can display HTML tags In Laravel blade using using {!! $data !!} and @php echo html_entity_decode($data) @endphp. It helps you to display the HTML tags as tags not as text.

What is Blade HTML?

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.


2 Answers

You need to use

{!! $text !!} 

The string will auto escape when using {{ $text }}.

like image 125
terry low Avatar answered Sep 22 '22 06:09

terry low


For laravel 5

{!!html_entity_decode($text)!!} 

Figured out through this link, see RachidLaasri answer

like image 21
Praveen Dabral Avatar answered Sep 22 '22 06:09

Praveen Dabral