首页 > 解决方案 > 如何在刀片中显示带有数据库变量的 html

问题描述

我在数据库中有带有变量的 HTML 代码我想在刀片中显示带有 html 设计和变量数据的代码

数据库代码

<ul class="dropdown-menu dropdown-menu-right">
<li>
@foreach ($block->options as $option)
<a>{{$option->name}}</a>
@endforeach
</li>
</ul>

刀片视图

@foreach ($blocks as $block)
{!! $block !!}
@endforeach

我已经显示了 html 代码,但变量仍然是字符串

在此处输入图像描述

标签: htmldatabaselaravelvariableslaravel-blade

解决方案


htmlentities()在将数据保存到数据库之前,请在 html 代码字符串上使用 php 内置函数,如下所示:

$html_code = '<p class="any-class">Lorem Ipsum / Random Text</p>';
$encode = htmlentities($html_code);
/* Insert the $encode in the database cell where you want to store html */

然后当使用刀片显示带有 html 标记的单元格数据时,您执行此操作{{!! html_entity_decode($block) !!}}

这会很好用!!!干杯!!


推荐阅读