Laravel 5: Display HTML with Blade

后端 未结 20 2174
栀梦
栀梦 2020-11-22 16:21

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

$text = \'

Lorem ipsum dolor

相关标签:
20条回答
  • 2020-11-22 16:50

    you can do with many ways in laravel 5..

    {!! $text !!}
    
    {!! html_entity_decode($text) !!}
    
    0 讨论(0)
  • 2020-11-22 16:52

    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.

    0 讨论(0)
  • 2020-11-22 16:53

    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.

    0 讨论(0)
  • 2020-11-22 16:54

    If you use the Bootstrap Collapse class sometimes {!! $text !!} is not worked for me but {{ html_entity_decode($text) }} is worked for me.

    0 讨论(0)
  • 2020-11-22 16:57

    You can try this:

    {!! $text !!}
    

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

    0 讨论(0)
  • 2020-11-22 16:58

    Try this, It's worked:

    @php 
       echo $text; 
    @endphp
    
    0 讨论(0)
提交回复
热议问题