【问题标题】:Laravel: Change Blade Parser {{ }} to Custom Parser?Laravel:将刀片解析器 {{ }} 更改为自定义解析器?
【发布时间】:2017-10-02 07:29:26
【问题描述】:

我有包含这样的 URL 标记的纯文本:

$string = "This is a good \[example\]\(http://www.example.com\) for my problem."

现在我想将此变量 $string 传递给 Blade 模板并像这样解析它:

This is a good <a href='http://www.example.com'>example</a> for my problem.

但是,由于 {{ }} 使用 htmspecialchars,输出看起来像这样:

This is a good &lt ;a href='http://www.example.com'&gt ;example&lt ;/a&gt ; for my problem.

目前,我使用控制器中的自定义函数将字符串解析为所需的输出,并将其呈现在带有 {! $parsed_string !} 的 Blade 模板中。有没有办法改变 Laravel 中函数 {{}} 的默认行为?

【问题讨论】:

  • 使用 {!! !!} 代替!

标签: php laravel-5


【解决方案1】:

使用{!! $yourVariable !!}可以让你输出html元素。

来自 laravel 文档:

显示未转义的数据

默认情况下,Blade {{ }} 语句会自动通过 PHP 的 htmlspecialchars 函数可以防止 XSS 攻击。如果你不 希望您的数据被转义,您可以使用以下语法:

Hello, {!! $name !!}.

【讨论】:

    【解决方案2】:

    如果您不想转义 {{ }} 中的任何特殊字符,可以使用
    {!!....您的 HTML 代码....!!}

    【讨论】:

      猜你喜欢
      • 2016-06-24
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 2012-03-16
      • 1970-01-01
      • 2016-12-29
      相关资源
      最近更新 更多