Laravel 5: Display HTML with Blade

后端 未结 20 2416
栀梦
栀梦 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: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.

提交回复
热议问题