Laravel 5: Display HTML with Blade

谁说胖子不能爱 提交于 2019-11-26 01:33:42

问题


I have a string returned to one of my views, like this:

$text = \'<p><strong>Lorem</strong> ipsum dolor <img src=\"images/test.jpg\"></p>\'

I\'m trying to display it with Blade:

{{$text}}

However, the output is a raw string instead of rendered HTML. How do I display HTML with Blade in Laravel 5?

PS. PHP echo() displays the HTML correctly.


回答1:


You need to use

{!! $text !!}

The string will auto escape when using {{ $text }}.




回答2:


For laravel 5

{!!html_entity_decode($text)!!}

Figured out through this link, see RachidLaasri answer




回答3:


You can try this:

{!! $text !!}

You should have a look at: http://laravel.com/docs/5.0/upgrade#upgrade-5.0




回答4:


Please use

{!! $test !!} 

Only in case of HTML while if you want to render data, sting etc. use

{{ $test }}

This is because when your blade file is compiled

{{ $test }} is converted to <?php echo e($test) ?> while

{!! $test !!} is converted to <?php echo $test ?>




回答5:


There is another way. If object purpose is to render html you can implement \Illuminate\Contracts\Support\Htmlable contract that has toHtml() method.

Then you can render that object from blade like this: {{ $someObject }} (note, no need for {!! !!} syntax).

Also if you want to return html property and you know it will be html, use \Illuminate\Support\HtmlString class like this:

public function getProductDescription()
{
    return new HtmlString($this->description);
}

and then use it like {{ $product->getProductDescription() }}.

Of course be responsible when directly rendering raw html on page.




回答6:


Try this. It worked for me.

{{ html_entity_decode($text) }}

In Laravel Blade template, {{ }} wil escape html. If you want to display html from controller in view, decode html from string.




回答7:


You can use {!! $text !!} for render HTML code in Laravel

{!! $text !!}

If you use

{{ $text }}

It will not render HTML code and print as a string.




回答8:


Use {!! $text !!}to display data without escaping it. Just be sure that you don’t do this with data that came from the user and has not been cleaned.




回答9:


its a simple

{!! $text !!}

laravel compile as a dom element and {{$text}} print as a string




回答10:


you can do with many ways in laravel 5..

{!! $text !!}

{!! html_entity_decode($text) !!}



回答11:


You can do that using three ways first use if condition like below

{!! $text !!}

The is Second way

<td class="nowrap">
@if( $order->status == '0' )
    <button class="btn btn-danger">Inactive</button>
@else
    <button class="btn btn-success">Active</button>
@endif
</td>

The third and proper way for use ternary operator on blade

<td class="nowrap">
      {!! $order->status=='0' ? 
          '<button class="btn btn-danger">Inactive</button> : 
          '<button class="btn btn-success">Active</button> !!}
</td>

I hope the third way is perfect for used ternary operator on blade.




回答12:


If you want to escape the data use

{{ $html }}

If don't want to escape the data use

{!! $html !!}

But till Laravel-4 you can use

{{ HTML::link('/auth/logout', 'Sign Out', array('class' => 'btn btn-default btn-flat')) }}

When comes to Laravel-5

{!! HTML::link('/auth/logout', 'Sign Out', array('class' => 'btn btn-default btn-flat')) !!} 

You can also do this with the PHP function

{{ html_entity_decode($data) }}

go through the PHP document for the parameters of this function

html_entity_decode - php.net




回答13:


This works fine for Laravel 5.6

<?php echo "$text"; ?>

In a different way

{!! $text !!}

It will not render HTML code and print as a string.

For more details open link:- Display HTML with Blade




回答14:


For who using tinymce and markup within textarea:

{{ htmlspecialchars($text) }}



回答15:


I have been there and it was my fault. And very stupid one.

if you forget .blade extension in the file name, that file doesn't understand blade but runs php code. You should use

/resources/views/filename.blade.php

instead of

/resources/views/filename.php

hope this helps some one




回答16:


Try this, It's worked:

@php 
   echo $text; 
@endphp


来源:https://stackoverflow.com/questions/29253979/laravel-5-display-html-with-blade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!