Laravel Blade @yield 和转义

Laravel Blade @yield and escaping

在 Laravel 5 我有一个主模板包含:

<title>@yield('title') | Site Name</title>

在我看来我有:

@extends('master')

@section('title', $client->name)

...

问题是,@yield 没有转义传递给它的数据。到目前为止,我找到的唯一解决方案是像这样手动转义数据:

@section('title', e($client->name))

这是最好的方法吗?这意味着我必须在使用变量的每个视图上手动转义数据。我没有看到从主模板中转义 @yield 指令的方法 - 在 @yield 周围使用 {{ }}e() 不起作用。

@section('title')
 {{ $client->name }}
@stop

为什么不执行以下操作:

@section('title') {{$client->name}} @endsection

这至少与您其余视图中的转义数据一致。按照你的方式,你很可能会错过 e()。有了上面的,你渲染视图的时候,如果你没有逃脱,你就可以立即看到。

更新

如何使用原始 PHP:

<title> <?php echo e($__env->yieldContent('title')); ?> | Site Name</title>

这是 blade 模板引擎替换 @yield 的内容,但我添加了转义助手。

这应该意味着您不需要转义 @section。我觉得这个可行,还没试过。

在我的例子中,问题是它实际上转义了我传递的字符串,如下所示:

@section('title', 'String with \' string')

在实际 HTML 的结果中,您会看到 &#039; 而不是实际的 '

解决方法是编写如下部分:

@section('title'){!! "String with ' string" !!}@endsection