Atunde Arisekola
Atunde Arisekola

Reputation: 31

How to pass html element from Laravel controller to view

I want to pass an HTML element from the controller to view in Laravel

For example:

$list = '<p>card</card>';

return view('home', ['list' => $list]);

Upvotes: 3

Views: 15498

Answers (5)

kapitan
kapitan

Reputation: 2212

Controller:

$myVariable = '<b>These are bold texts</b>';

return view('home', ['myVariable' => $myVariable ]);

Blade:

{{ $myVariable }} output is: <b>These are bold texts</b>

{!! $myVariable !!} output is: These are bold texts

Also, if the variable on your controller is the same as the variable name you want to pass on the blade, you can use compact like so:

return view('home', compact('myVariable'));

Upvotes: 0

Charles
Charles

Reputation: 276

This is one way

      $data = “hello world” ;
      $ value = 15;

       Return view(‘hello’, compact(‘data’,’value’);


Upvotes: 0

Yves Kipondo
Yves Kipondo

Reputation: 5603

When you pass data in your controller to the view which is render. When you want to show that data un your view template. You have two possibility the first one is escaping everything by passing it to the htmlspecialchars méthode which remove any HTML tags. And the second method show data whitout escaping it.

  1. This one will escape HTML tags {{ data_provide_in_the_controller }}

  2. This will output any HTML tags contain in the variable provide

    {!! data_provide_in_the_controller!!}

Upvotes: 1

Eliott
Eliott

Reputation: 262

I don't know if you are going to need to pass more HTML, if you do you can try this way

View:

<p>card</card>
<p>card</card>
<p>card</card>
More HTML 

Controller:

$view = view('your.view')->render();

Upvotes: 2

Ijas Ameenudeen
Ijas Ameenudeen

Reputation: 9259

The way you pass from the controller is correct. You can render HTML in your view as,

{!! $list !!}

Reference: https://laravel.com/docs/5.7/blade#displaying-data

Fiddle: https://implode.io/XnjDKO

Upvotes: 11

Related Questions