What is the difference between {{ }} and {!! !!} in laravel blade files?
Asked Answered
T

6

61

In the laravel framework we can use blade to add PHP code in html file.
We are using both {{ }} and {!! !!} syntax in blade files of Laravel.
What is the difference between them?

Tew answered 27/1, 2016 at 7:16 Comment(0)
L
129

Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks.

If you pass data from your Controller to a View with some HTML styling like:

$first = "<b>Narendra Sisodia</b>";

And it is accessed, within Blade, with {{ $first }} then the output'll be:

<b>Narendra Sisodia</b>

But if it is accessed with {!! $first !!} then the output'll be:

Narendra Sisodia

Leeleeann answered 27/1, 2016 at 7:35 Comment(1)
Highly impressive solution and given example, thanks Narendrasingh Sisodia :) โ€“ Uncounted
G
15

If you don't want the data to be escaped then use {!! !!} else use {{ }}.

Gownsman answered 27/1, 2016 at 7:19 Comment(0)
S
9

To escape data use

{{ $data }}

If you don't want the data to be escaped use below

{!! $data !!}
Soper answered 14/8, 2017 at 16:3 Comment(0)
D
7

from the documentation: https://laravel.com/docs/5.1/blade

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.
Doig answered 27/1, 2016 at 7:20 Comment(0)
S
3

Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks.

You can see more here:https://laravel.com/docs/master/blade

Stylish answered 27/1, 2016 at 7:21 Comment(0)
U
1

In Laravel {{ }} should pass data as plain text with HTML escaping while {!! !!} pass data as content as-is, without any HTML escaping.

For e.g: I have a $status = '<b>Active Status</b>'; variable which stores HTML content as a data.

Now if I write this data as {{ $status }} Then it will return as a plain text.

Expected Output is:

<b>Active Status</b>

But if I write this data as {!! $status !!} then it will return as HTML code.

Expected Output is:

๐—”๐—ฐ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฆ๐˜๐—ฎ๐˜๐˜‚๐˜€
Urial answered 4/9, 2023 at 10:56 Comment(0)

© 2022 - 2024 โ€” McMap. All rights reserved.