我有一个字符串返回到我的一个视图,像这样:

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

我试图用Blade显示它:

{{$text}}

但是,输出是一个原始字符串,而不是呈现的HTML。我如何显示HTML与刀片在Laravel?

PS. PHP echo()正确显示HTML。


当前回答

你可以用很多方法在laravel ..

{!! $text !!}

{!! html_entity_decode($text) !!}

其他回答

对于拉拉维尔 5

{!!html_entity_decode($text)!!}

通过这个链接找到答案,见RachidLaasri的答案

请使用

{!! $test !!} 

只有在HTML的情况下,而如果你想呈现数据,sting等使用

{{ $test }}

这是因为在编译刀片文件时

{{$test}}被转换为<?PHP echo e($test) 而

{! !美元的测试! !}被转换为<?PHP echo $test ?>

在控制器。

$your_variable = '';
$your_variable .= '<p>Hello world</p>';

return view('viewname')->with('your_variable', $your_variable)

如果你不希望你的数据被转义,你可以使用下面的语法:

{!! $your_variable !!}

输出

Hello world

{! !!!不安全。

阅读此处:https://laravel.com/docs/5.6/blade#displaying-data

你可以试试:

    @php 
     echo $variable; 
    @endphp

你可以使用以下三种方法首先使用if条件

{!! $text !!}

第二种方法

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

第三,在叶片上使用三元算符的正确方法

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

我希望第三种方法是完美的使用三元算子刀片。