Laravel 5 echo out session variable containing html in blade
Asked Answered
C

5

17

I did a redirect in laravel:

return redirect('admin')->with($returnData);

$returnData is a string that contains a bootstrap info div with the result from the controller. Almost everything is working except when the page loads again it shows the html on the page as if it were text, brackets and everything. If I use this:

@if(!empty(Session::get('error'))) {{ Session::get('error')}} @endif

Then it shows is as pure text. If I change it to

<?php if(!empty(Session::get('error'))) { echo Session::get('error'); } ?>

It works fine. Im ok keeping it like this but I would rather utilize Blade / Laravel as its supposed to be used so I was wondering if there is a way to have the @if statement show the rendered html and not the text version?

Cusk answered 23/8, 2015 at 22:38 Comment(1)
Not an answer, but you should use {{!! [PHP code goes here] !!}} to execute code. Blade templates give you that freedom to use a much more clean syntax than surrounding it with PHP tags.Curch
B
47

I would recommend returning just the error message then in your view create the div. So if you were to change the layout of the view, you would it in one place.

@if(Session::has('error'))
<div class="alert alert-danger">
  {{ Session::get('error')}}
</div>
@endif

hope this help.

Bastard answered 23/8, 2015 at 23:24 Comment(0)
C
6

May this example will help you. Try this

 @if (session('Error'))
     <div class="alert alert-success">
         {{ session('Error') }}
     </div>
@endif
Carpal answered 6/3, 2018 at 10:16 Comment(0)
D
5

To show the rendered HTML you should use {!! $variable->coontent !!} in your view, and this gonna convert your HTML text to render

Duclos answered 23/8, 2015 at 22:43 Comment(0)
T
2

If you want to display plain text from error without any HTML entities you can simply use:

{{ Session::get('error') }}

or

{{ session('error') }}

If you have HTML entities in your variable then use:

{!! Session::get('error') !!}
Timothytimour answered 7/1, 2021 at 11:38 Comment(0)
S
0

Try changing your blade code to following.

@if(!empty(Session::get('error'))) 
    {!! Session::get('error') !!} 
@endif
Swedenborgianism answered 23/8, 2015 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.