online software programming courses

5 different ways to render HTML in Laravel

1. Using double curly braces

If your controller passes a string variable to laravel blade,

$str="This is my html render";

 and if you want to display text or paragraphs which is passed from the controller, you should do like this


Within double curly braces, you should write your variable that is passed from controller to blade, and you are good to go and you will see

This is my html render

For explanation,  the above statement is the same as the line below. PHP auto inverts from {{$str}} to

<?php echo e($str); ?>

Here e() is convenience wrapper for echo() function. But it's already deprecated in Laravel. 

2. Using single curly braces and exclamation marks

Say, you have a variable $str in controller with some html tag like the below one and you want to send to blade using compact() function in laravel. 

$str="<p>This is my text</p>"

After receiving to the blade, variable $str contains HTML tag. Then you must use a single curly brace in each side and double exclamation marks on both side of the variable, and this is the most common ways of laravel not escaping  HTML in the blade template


And then your output would be like below. Remember you use  single curly braces and two exclamation marks to display your variable value.

This is my test

So {{}} prints the string as it is, but {!! !!} will render html.

3. Using html_entity_decode() function

This is our good old PHP function which does a lot of work for you.  This function removes HTML entities from a string, what is HTML entities? They are special strings that starts with &(ampersend) sign and ends with ;(semicolon).  See the below code which contains a few HTML entities. 

public function show(){
        $str = '&lt;a href=&quot;;&gt;;/a&gt;';
    	return view('frontend.view', compact( 'str'));

In the above function $str contains HTML entities and variable $str after being passed through html_entity_decode() function in blade,


it produces the output like below

 So next time if you have string that contains HTML entities, just use this HTML entity decode function and your problems would be solved.

4. Use traditional PHP opening 

You can render HTML in blade using traditional PHP opening and closing tag. You can write 

	echo $string;

In your blade. The variable $string is coming from your controller. But it's not recommended to do like this. This is more like spaghetti code, where you put PHP and HTML together. 

5. Use Laravel PHP directive

Laravel is famous for it's clean and readable PHP syntax. If the method mentioned in 4, looks like a spaghetti code for you then try the below one

	echo $text;

It's one of my favorite way of writing PHP in Laravel blade.

Things to remember about laravel blade escape html

If you want to render a string and it does not contain any special characters or html tags then use {{$str}}

If your variable string includes html tags but not any html entities then just use {!!$str!!} for unescaping HTML. Becuase {{$str}} escape HTML tags meaning that, {{$str}} will ecapse html.

If you variable contains both html tags and html entities then use {!!html_entity_decode($str)!!}