Laravel Blade 文件中的 {{ }} 和 {{{ }}} 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37519403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:54:26  来源:igfitidea点击:

What is the difference between {{ }} and {{{ }}} in laravel blade files?

laravel

提问by hkguile

In the laravel framework we can use blade to add PHP code in html file. We are using both {{ }} and {{{ }}}syntax in blade files of Laravel. What is the difference between them?

在laravel框架中,我们可以使用blade在html文件中添加PHP代码。我们{{ }} and {{{ }}}在 Laravel 的刀片文件中同时使用这两种语法。它们之间有什么区别?

i have a code in a blade file like this

我在这样的刀片文件中有一个代码

{{{ $errors->has('slider') ? 'has-error' : '' }}}

回答by Rok Novosel

Before Laravel 5:

在 Laravel 5 之前:

  • {{{ $var }}}escapes the contents of $var
  • {{ $var }}echoes $var unescaped
  • {{{ $var }}}转义 $var 的内容
  • {{ $var }}回声 $var 未转义

Since Laravel 5:

从 Laravel 5 开始:

  • {!! $var !!}echoes $var unescaped
  • {{ $var }}escapes the contents of $var
  • {!! $var !!}回声 $var 未转义
  • {{ $var }}转义 $var 的内容

回答by Alexey Mezenin

There is no difference between {{ }}and {{{ }}}in Laravel 5 at all.

Laravel 5 中的{{ }}和完全没有区别{{{ }}}

In version 4 it included the following two styles: “{{” and “{{{“. The double curly bracket was a raw echo and the triple curly bracket escaped.

Currently in 5.0 both the double and triple curly brackets escape the variable and a new “{!! $var !!}” is for raw.

在第 4 版中,它包括以下两种样式:“{{”和“{{{”。双花括号是一个原始的回声,而三花括号逃脱了。

目前在 5.0 中,双花括号和三花括号都转义了变量和一个新的“{!! $var !!}” 是原始的。

https://laravel-news.com/2014/09/laravel-5-0-blade-changes/

https://laravel-news.com/2014/09/laravel-5-0-blade-changes/

回答by tuytoosh

{{{renders HTML data but {{shows data and elements without rendering.

{{{呈现 HTML 数据但{{不呈现数据和元素。

ex :

前任 :

$str = "<h3>tuytoosh is here</h3>";

$str = "<h3>tuytoosh is here</h3>";

{{ $str }}output : <h3>tuytoosh is here</h3>

{{ $str }}输出 : <h3>tuytoosh is here</h3>

{{{ $str }}}output :

{{{ $str }}}输出 :

tuytoosh is here

tuytoosh 在这里

回答by Rifki

In Laravel 4{{{ $var }}}is used to render data by escaping HTML entities that uses PHP htmlentitiesfunction to prevent XSS attacks.

Laravel 4{{{ $var }}}中,通过转义 HTML 实体来渲染数据,使用 PHPhtmlentities函数来防止 XSS 攻击。

{{ $var }}is used to render data without escaping HTML entities.

{{ $var }}用于在不转义 HTML 实体的情况下呈现数据。

In Laravel 5it slightly different you can use either {{ $var }}or {{{ $var }}}to escape data and to render unescaped data you can use {!! $var !!}.

Laravel 5 中,它略有不同,您可以使用{{ $var }}{{{ $var }}}来转义数据并渲染您可以使用的未转义数据{!! $var !!}

回答by Arthur

Something I don't see mentioned here but which can be handy to know. If you have a translation text with a parameter that should become a Vue variable in the resulting html then the triple curly braces come in handy.

我没有看到这里提到的东西,但可以很方便地知道。如果您的翻译文本的参数应该成为结果 html 中的 Vue 变量,那么三重花括号就派上用场了。

E.g.:

例如:

{{{ __('This window will close in :timeout seconds', ['timeout' => '@{{timeoutSecs}}']) }}}

Attempting this with any other combination of curly braces will throw some error.

用任何其他花括号组合尝试这样做会引发一些错误。