【问题标题】:Laravel not rendering htmlLaravel 不渲染 html
【发布时间】:2018-10-10 10:08:51
【问题描述】:

我尝试使用数据库中的内容呈现 html 标记。 这是一堆带有简单<a> 标签的文本。

这就是它在数据库字段中的设置方式。数据库字段类型为varchar(200)

排序规则是utf8_unicode_ci

这是字段的值:

blablabla <a href="mailto:info@atmox.nl">info@atmox.nl</a> blablabla

我尝试仅使用 {!! !!} 刀片语法,但它只会将标记呈现为纯文本。最终我尝试了html_entity_decodehtmlspecialchars_decode 函数,但结果是一样的。纯文本。

这是html部分

<p>{!! $baan->descriptiond !!}</p>

【问题讨论】:

    标签: php html laravel


    【解决方案1】:

    尝试在您的数据上使用htmlentities($baan-&gt;descriptiond)html_entity_decode($string) 呈现,然后使用 {{ $baan->descriptiond }} 呈现 html。

    只需使用普通的 laravel 刀片:

    {{$baan->descriptiond}}
    

    【讨论】:

    • @rick,你试过了吗?
    • 现在试试,普通刀片不起作用,现在我正在尝试htmlentities()html_entity_decode() 功能,但是,我需要将它们应用于$baan-&gt;descriptiond 吗?由于将其应用于我解析的对象将不起作用..
    • 是的,适用于$baan-&gt;descriptiond,它应该可以工作。
    • 喜欢这个? $baan-&gt;descriptiond = htmlentities($baan-&gt;descriptiond); $baan-&gt;descriptiond = html_entity_decode($baan); 将其渲染为:{{ $baan-&gt;descriptiond }}
    • echo htmlentities($baan-&gt;descriptiond);
    【解决方案2】:

    你真的应该能够做到这一点:

    <p>{!! html_entity_decode($baan->descriptiond) !!}</p>
    

    假设$baan-&gt;descriptiond 类似于:

    &lt;a href=&quot;mailto:info@atmox.nl&quot;&gt;info@atmox.nl&lt;/a&gt;
    

    【讨论】:

    • $baan-&gt;descriptiond 包含一些文本,例如:You can mail us at &amp;lt;a href=&amp;quot;mailto:info@atmox.nl&amp;quot;&amp;gt;info@atmox.nl&amp;lt;/a&amp;gt; or try the form below.
    • 试过你的解决方案,但它仍然显示纯文本,如:you can mail us at &lt;a href="mailto:info@entric.nl"&gt;info@entric.nl&lt;/a&gt; or use the form below:
    • 我在 Laravel 5.6 中对此进行了测试,并且可以正常工作。您目前在这里有 3 个答案告诉您同样的事情。
    • 尝试了所有 3 个答案,但无济于事,是否可能与数据库字段有关?
    • 你用原始数据字符串测试了吗?
    【解决方案3】:

    你需要这样做

    {!! $text !!}
    

    当你执行 {{ }}

    时字符串会自动转义

    对于 laravel 5

    {!!html_entity_decode($text)!!}
    

    【讨论】:

    • 不起作用..我的数据库字段可能有问题吗?
    • @Rick_Jellema 是的,也许可以,也检查一次
    • 数据库没问题,我可以尝试制作一个新的 Laravel 项目来测试它是否在那里工作,我的 laravel 设置可能有问题。稍后再回来查看。
    猜你喜欢
    • 2020-01-16
    • 2015-09-12
    • 2018-12-09
    • 2019-07-24
    • 1970-01-01
    • 2021-06-02
    • 2020-07-11
    • 2021-05-09
    • 2017-06-04
    相关资源
    最近更新 更多