Laravel 5:使用 Blade 显示 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29253979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:56:26  来源:igfitidea点击:

Laravel 5: Display HTML with Blade

laravellaravel-5blade

提问by lesssugar

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:

我正在尝试用 Blade 显示它:

{{$text}}

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

但是,输出是原始字符串,而不是呈现的 HTML。如何在 Laravel 5 中使用 Blade 显示 HTML?

PS. PHP echo()displays the HTML correctly.

附注。PHPecho()正确显示 HTML。

回答by terry low

You need to use

你需要使用

{!! $text !!}

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

使用时字符串将自动转义{{ $text }}

回答by Praveen Dabral

For laravel 5

对于laravel 5

{!!html_entity_decode($text)!!}

Figured out through this link, see RachidLaasri answer

通过此链接弄清楚,请参阅 RachidLaasri 答案

回答by rap-2-h

You can try this:

你可以试试这个:

{!! $text !!}

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

你应该看看:http: //laravel.com/docs/5.0/upgrade#upgrade-5.0

回答by Shubham Bansal

Please use

请用

{!! $test !!} 

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

仅在 HTML 的情况下,如果您想呈现数据、刺痛等,请使用

{{ $test }}

This is because when your blade file is compiled

这是因为当你的刀片文件被编译时

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

{{ $test }}转换为<?php echo e($test) ?>while

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

{!! $test !!}转换为 <?php echo $test ?>

回答by Damir Miladinov

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

还有另一种方法。如果对象目的是呈现 html,您可以实现\Illuminate\Contracts\Support\Htmlable具有toHtml()方法的合同。

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

然后您可以像这样从刀片渲染该对象:({{ $someObject }}注意,不需要{!! !!}语法)。

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

此外,如果您想返回 html 属性并且您知道它将是 html,请使用如下\Illuminate\Support\HtmlString类:

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

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

然后像{{ $product->getProductDescription() }}.

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

直接在页面上渲染原始 html 时当然要负责。

回答by Mohammed Safeer

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.

在 Laravel Blade 模板中,{{}} 将转义 html。如果要在视图中显示控制器中的 html,请从字符串中解码 html。

回答by PPL

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

您可以使用 {!!$text !!} 用于在 Laravel 中渲染 HTML 代码

{!! $text !!}

If you use

如果你使用

{{ $text }}

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

它不会呈现 HTML 代码并打印为字符串。

回答by Ravindra Bhanderi

its a simple

这是一个简单的

{!! $text !!}

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

laravel 编译为 dom 元素并{{$text}}打印为字符串

回答by Patrick Luy

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.

使用{!! $text !!}无需逃避它显示的数据。请确保不要对来自用户且尚未清理的数据执行此操作。

回答by Jignesh Joisar

you can do with many ways in laravel 5..

你可以在 Laravel 5 中用很多方法做..

{!! $text !!}

{!! html_entity_decode($text) !!}