用 Blade 显示 HTML 显示 HTML 代码
Displaying HTML with Blade shows the HTML code
我有一个返回到我的视图之一的字符串,如下所示:
$text = '<p><strong>Lorem</strong> ipsum dolor <img src="images/test.jpg"></p>'
我正在尝试用 Blade 显示它:
{{$text}}
但是,输出是原始字符串而不是渲染的 HTML。如何在 Laravel 中使用 Blade 显示 HTML?
PS。 PHP echo()
正确显示 HTML。
您需要使用
{!! $text !!}
使用{{ $text }}
时字符串会自动转义。
你可以试试这个:
{!! $text !!}
对于laravel 5
{!!html_entity_decode($text)!!}
通过这个 link 想通了,参见 RachidLaasri 的回答
试试这个。它对我有用。
{{ html_entity_decode($text) }}
在 Laravel Blade 模板中,{{ }} 将转义 html。如果要在视图中显示来自控制器的 html,请从字符串中解码 html。
请使用
{!! $test !!}
只有在 HTML 的情况下,如果你想渲染数据,刺痛等使用
{{ $test }}
这是因为当你的 blade 文件被编译时
{{ $test }}
转换为 <?php echo e($test) ?>
而
{!! $test !!}
转换为 <?php echo $test ?>
还有一个办法。如果 object 目的是呈现 html,您可以实施具有 toHtml()
方法的 \Illuminate\Contracts\Support\Htmlable
合同。
然后您可以像这样从 blade 渲染 object:{{ $someObject }}
(注意,不需要 {!! !!}
语法)。
此外,如果你想 return html 属性 并且你知道它将是 html,请像这样使用 \Illuminate\Support\HtmlString
class :
public function getProductDescription()
{
return new HtmlString($this->description);
}
然后像{{ $product->getProductDescription() }}
一样使用它。
在页面上直接渲染rawhtml当然要负责
如果你想转义数据使用
{{ $html }}
如果不想转义数据使用
{!! $html !!}
但是在 Laravel-4 之前你可以使用
{{ HTML::link('/auth/logout', 'Sign Out', array('class' => 'btn btn-default btn-flat')) }}
什么时候Laravel-5
{!! HTML::link('/auth/logout', 'Sign Out', array('class' => 'btn btn-default btn-flat')) !!}
您也可以使用 PHP 函数
{{ html_entity_decode($data) }}
通过 PHP 文档获取此函数的参数
使用{!! $text !!}
显示数据而不转义。请确保您不会对来自用户且尚未清理的数据执行此操作。
这适用于 Laravel 5.6
<?php echo "$text"; ?>
换种方式
{!! $text !!}
它不会呈现 HTML 代码并打印为字符串。
有关更多详细信息,请打开 link:- Display HTML with Blade
对于在 textarea 中使用 tinymce 和标记的人:
{{ htmlspecialchars($text) }}
我去过那里,这是我的错。还有一个很蠢。
如果您忘记了文件名中的 .blade 扩展名,则该文件不理解 blade 但运行 php 代码。你应该使用
/resources/views/filename.blade.php
而不是
/resources/views/filename.php
希望这对某些人有所帮助
您可以使用三种方法来做到这一点,首先使用如下条件
{!! $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>
在 blade
上使用三元运算符的第三种正确方法
<td class="nowrap">
{!! $order->status=='0' ?
'<button class="btn btn-danger">Inactive</button> :
'<button class="btn btn-success">Active</button> !!}
</td>
我希望第三种方式非常适合在 blade 上使用的三元运算符。
您可以在 laravel 5..
中使用多种方法
{!! $text !!}
{!! html_entity_decode($text) !!}
试试这个,成功了:
@php
echo $text;
@endphp
如果您有时使用 Bootstrap 折叠 class {!! $text !!}
对我不起作用,但 {{ html_entity_decode($text) }}
对我有用。
为了进一步说明,Blade {{ }}
语句中的代码自动通过 php 提供的 htmlspecialchars()
函数传递。此函数接受一个字符串,并将查找 HTML 使用的所有保留字符。保留字符为 &
<
>
和 "
。然后它将用它们的 HTML 实体变体替换这些保留字符。以下是:
|---------------------|------------------|
| Character | Entity |
|---------------------|------------------|
| & | & |
|---------------------|------------------|
| < | < |
|---------------------|------------------|
| > | > |
|---------------------|------------------|
| " | " |
|---------------------|------------------|
例如,假设我们有以下 php 语句:
$hello = "<b>Hello</b>";
传递到 blade 因为 {{ $hello }}
会产生您传递的文字字符串:
<b>Hello</b>
在引擎盖下,它实际上会回显为 <b>Hello<b>
如果我们想绕过它并实际将其呈现为粗体标记,我们通过添加转义语法 blade 提供:
来转义 htmlspecialchars()
函数
{!! $hello !!}
请注意,我们只使用了一个大括号。
以上的输出将产生:
你好
我们还可以利用 php 提供的另一个方便的函数,即 html_entity_decode()
函数。这会将 HTML 实体转换为它们受尊重的 HTML 字符。将其视为 htmlspecialchars()
的反面
例如假设我们有以下 php 语句:
$hello = "<b> Hello <b>";
我们现在可以将此函数添加到我们的转义 blade 语句中:
{!! html_entity_decode($hello) !!}
这将采用 HTML 实体 <
并将其解析为 HTML 代码 <
,而不仅仅是一个字符串。
这同样适用于大于实体 >
这将产生
你好
转义的首要目的是为了避免 XSS 攻击。所以在使用转义语法时要非常小心,尤其是 如果您的应用程序中的用户自己提供HTML,他们可以随意注入自己的代码。
在控制器上。
$your_variable = '';
$your_variable .= '<p>Hello world</p>';
return view('viewname')->with('your_variable', $your_variable)
如果您不希望您的数据被转义,您可以使用以下语法:
{!! $your_variable !!}
输出
Hello world
By default, Blade {{ }}
statements are automatically sent through PHP's htmlspecialchars
function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
根据 the doc,您必须执行以下操作才能在 Blade 文件中呈现 html:
{!! $text !!}
Be very careful when echoing content that is supplied by users of your application. You should typically use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.
当您的数据包含 HTML 标签时,请使用
{!! $text !!}
如果您的数据不包含 HTML 标签,则使用
{{ $text }}
{!! !!} 不安全。
阅读此处:https://laravel.com/docs/5.6/blade#displaying-data
你可以试试:
@php
echo $variable;
@endphp
我有一个返回到我的视图之一的字符串,如下所示:
$text = '<p><strong>Lorem</strong> ipsum dolor <img src="images/test.jpg"></p>'
我正在尝试用 Blade 显示它:
{{$text}}
但是,输出是原始字符串而不是渲染的 HTML。如何在 Laravel 中使用 Blade 显示 HTML?
PS。 PHP echo()
正确显示 HTML。
您需要使用
{!! $text !!}
使用{{ $text }}
时字符串会自动转义。
你可以试试这个:
{!! $text !!}
对于laravel 5
{!!html_entity_decode($text)!!}
通过这个 link 想通了,参见 RachidLaasri 的回答
试试这个。它对我有用。
{{ html_entity_decode($text) }}
在 Laravel Blade 模板中,{{ }} 将转义 html。如果要在视图中显示来自控制器的 html,请从字符串中解码 html。
请使用
{!! $test !!}
只有在 HTML 的情况下,如果你想渲染数据,刺痛等使用
{{ $test }}
这是因为当你的 blade 文件被编译时
{{ $test }}
转换为 <?php echo e($test) ?>
而
{!! $test !!}
转换为 <?php echo $test ?>
还有一个办法。如果 object 目的是呈现 html,您可以实施具有 toHtml()
方法的 \Illuminate\Contracts\Support\Htmlable
合同。
然后您可以像这样从 blade 渲染 object:{{ $someObject }}
(注意,不需要 {!! !!}
语法)。
此外,如果你想 return html 属性 并且你知道它将是 html,请像这样使用 \Illuminate\Support\HtmlString
class :
public function getProductDescription()
{
return new HtmlString($this->description);
}
然后像{{ $product->getProductDescription() }}
一样使用它。
在页面上直接渲染rawhtml当然要负责
如果你想转义数据使用
{{ $html }}
如果不想转义数据使用
{!! $html !!}
但是在 Laravel-4 之前你可以使用
{{ HTML::link('/auth/logout', 'Sign Out', array('class' => 'btn btn-default btn-flat')) }}
什么时候Laravel-5
{!! HTML::link('/auth/logout', 'Sign Out', array('class' => 'btn btn-default btn-flat')) !!}
您也可以使用 PHP 函数
{{ html_entity_decode($data) }}
通过 PHP 文档获取此函数的参数
使用{!! $text !!}
显示数据而不转义。请确保您不会对来自用户且尚未清理的数据执行此操作。
这适用于 Laravel 5.6
<?php echo "$text"; ?>
换种方式
{!! $text !!}
它不会呈现 HTML 代码并打印为字符串。
有关更多详细信息,请打开 link:- Display HTML with Blade
对于在 textarea 中使用 tinymce 和标记的人:
{{ htmlspecialchars($text) }}
我去过那里,这是我的错。还有一个很蠢。
如果您忘记了文件名中的 .blade 扩展名,则该文件不理解 blade 但运行 php 代码。你应该使用
/resources/views/filename.blade.php
而不是
/resources/views/filename.php
希望这对某些人有所帮助
您可以使用三种方法来做到这一点,首先使用如下条件
{!! $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>
在 blade
上使用三元运算符的第三种正确方法<td class="nowrap">
{!! $order->status=='0' ?
'<button class="btn btn-danger">Inactive</button> :
'<button class="btn btn-success">Active</button> !!}
</td>
我希望第三种方式非常适合在 blade 上使用的三元运算符。
您可以在 laravel 5..
中使用多种方法{!! $text !!}
{!! html_entity_decode($text) !!}
试试这个,成功了:
@php
echo $text;
@endphp
如果您有时使用 Bootstrap 折叠 class {!! $text !!}
对我不起作用,但 {{ html_entity_decode($text) }}
对我有用。
为了进一步说明,Blade {{ }}
语句中的代码自动通过 php 提供的 htmlspecialchars()
函数传递。此函数接受一个字符串,并将查找 HTML 使用的所有保留字符。保留字符为 &
<
>
和 "
。然后它将用它们的 HTML 实体变体替换这些保留字符。以下是:
|---------------------|------------------|
| Character | Entity |
|---------------------|------------------|
| & | & |
|---------------------|------------------|
| < | < |
|---------------------|------------------|
| > | > |
|---------------------|------------------|
| " | " |
|---------------------|------------------|
例如,假设我们有以下 php 语句:
$hello = "<b>Hello</b>";
传递到 blade 因为 {{ $hello }}
会产生您传递的文字字符串:
<b>Hello</b>
在引擎盖下,它实际上会回显为 <b>Hello<b>
如果我们想绕过它并实际将其呈现为粗体标记,我们通过添加转义语法 blade 提供:
来转义htmlspecialchars()
函数
{!! $hello !!}
请注意,我们只使用了一个大括号。
以上的输出将产生:
你好
我们还可以利用 php 提供的另一个方便的函数,即 html_entity_decode()
函数。这会将 HTML 实体转换为它们受尊重的 HTML 字符。将其视为 htmlspecialchars()
例如假设我们有以下 php 语句:
$hello = "<b> Hello <b>";
我们现在可以将此函数添加到我们的转义 blade 语句中:
{!! html_entity_decode($hello) !!}
这将采用 HTML 实体 <
并将其解析为 HTML 代码 <
,而不仅仅是一个字符串。
这同样适用于大于实体 >
这将产生
你好
转义的首要目的是为了避免 XSS 攻击。所以在使用转义语法时要非常小心,尤其是 如果您的应用程序中的用户自己提供HTML,他们可以随意注入自己的代码。
在控制器上。
$your_variable = '';
$your_variable .= '<p>Hello world</p>';
return view('viewname')->with('your_variable', $your_variable)
如果您不希望您的数据被转义,您可以使用以下语法:
{!! $your_variable !!}
输出
Hello world
By default, Blade
{{ }}
statements are automatically sent through PHP'shtmlspecialchars
function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
根据 the doc,您必须执行以下操作才能在 Blade 文件中呈现 html:
{!! $text !!}
Be very careful when echoing content that is supplied by users of your application. You should typically use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.
当您的数据包含 HTML 标签时,请使用
{!! $text !!}
如果您的数据不包含 HTML 标签,则使用
{{ $text }}
{!! !!} 不安全。
阅读此处:https://laravel.com/docs/5.6/blade#displaying-data
你可以试试:
@php
echo $variable;
@endphp