online software programming courses

Laravel Render Html in Controller 5 different ways

Laravel Render Html in Controller could be confusing for beginners. 

In this tutorial, I will show you how to pass HTML element from laravel controller to view. At the end of the tutorial, you will also learn how to return a view from controller to laravel blade.

Before you do any coding make sure that, you are naming your view files with .blade.php suffix and they are saved in resources/views folder

laravel-render-html-in-controller

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

{{$str}}

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

This is the most common way laravel render html if you don't have any tags.

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 on 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

{!!$str!!|}

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 {{ }} curly braces print the string as it is, but {!! !!} will render HTML from a string variable. And laravel blade double exclamation should be used when you have HTML tags in your string.

The above one is really useful when you want to render HTML from database to view blade.

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;https://www.w3schools.com&quot;&gt;w3schools.com&lt;/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,

{!!html_entity_decode($str)!!}

it produces the output like below

w3schools.com

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

You can also use {!!html_entity_decode($str)!!} when you want laravel to render HTML from database.

You use html_entity_decode in laravel if 

You want to render HTML from database

If your string has HTML entities

 

4. Use traditional PHP opening 

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

<?php
	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

@php
	echo $text;
@endphp

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)!!}

If you render any string that is coming from the database.

 

Laravel return HTML from controller

In general, we don't return an entire view from laravel controller. Even if you want to do it, you can. Let's see how to return entire HTML document strings from controller.

$str=
'<div style="width:100%, height:200, background-color:orange">
  <p> We love laravel. Its the best web framework </p>
  <a href="www.dbestech.com">The Best Technology</a>
</div>';

return view('greeting',  compact('str'));

After laravel return HTML in controller, the blade should be like below code sinppet

<html>
    <body>
       {!!$str!!}
    </body>
</html>