Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel setEscapedContentTags和setContentTags之间有什么区别?_Php_Templates_Laravel 5 - Fatal编程技术网

Php Laravel setEscapedContentTags和setContentTags之间有什么区别?

Php Laravel setEscapedContentTags和setContentTags之间有什么区别?,php,templates,laravel-5,Php,Templates,Laravel 5,为了更好地了解Laravel标记设置的工作原理,我尝试了以下方法: Blade::setContentTags('<x', 'x>'); Blade::setEscapedContentTags('<y', 'y>'); Blade::setRawTags('<z', 'z>'); Blade::setContentTags(“”); 刀片::setEscapedContentTags(“”); 刀片::setRawTags(“”); 在我的控制器构造函数

为了更好地了解Laravel标记设置的工作原理,我尝试了以下方法:

Blade::setContentTags('<x', 'x>');
Blade::setEscapedContentTags('<y', 'y>');
Blade::setRawTags('<z', 'z>');
Blade::setContentTags(“”);
刀片::setEscapedContentTags(“”);
刀片::setRawTags(“”);
在我的控制器构造函数中

在刀片视图中,我添加了

<div>
  <x 'test' x>
  <y 'test' y>
  <z 'test' z>
</div>

我清理了storage/framework/views文件夹并重新加载了页面

结果,在编译视图中,我得到了

<div>
    <?php echo e('test'); ?>

    <?php echo e('test'); ?>

    <?php echo 'test'; ?>

</div>  


如您所见,为setContentTags和setEscapedContentTags指定的标记编译的代码看起来是相同的。那么为什么我们需要这两个选项呢?

这是出于安全考虑。 默认情况下,
Blade
将为包含常规标记和转义标记的内容返回相同的结果
BladeCompiler
类具有值为
e(%s)
的受保护属性
$echoFormat
。当使用常规标记(在您的示例中是“x”)编译内容时,将使用此属性

该属性用作函数
e

/**
 * Escape HTML entities in a string.
 *
 * @param  string  $value
 * @return string
 */
function e($value)
{
    return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}
e
函数在使用转义标记编译内容时也会被调用(在您的例子中是“y”)

您还可以更改格式:

/**
 * Set the echo format to be used by the compiler.
 *
 * @param  string  $format
 * @return void
 */
public function setEchoFormat($format)
{
    $this->echoFormat = $format;
}
在默认设置下,如果在文本前面加上
@
符号,Blade将为带有
常规标记和
转义标记的内容返回不同的结果

用于查看参数

['str' => "<script>alert('name')</script>"]
['str'=>“警报('name')”]
带模板

<div>@{{ $str }}</div>
<div>@{{{ $str }}}</div>

<div>@{{"<a>Plain text</a>"}}</div>
<div>@{{{"<a>Plain text</a>"}}}</div>
@{{$str}
@{{{$str}}}
@{{“纯文本”}
@{{{“纯文本”}}
结果将是

<div>{{ $str }}</div>
<div>@&lt;script&gt;alert(&#039;name&#039;)&lt;/script&gt;</div>

<div>{{"<a>Plain text</a>"}}</div>
<div>@&lt;a&gt;Plain text&lt;/a&gt;</div>
{{$str}
@scriptalert(';name';)/script
{{“纯文本”}
@a主要文本/a
<div>{{ $str }}</div>
<div>@&lt;script&gt;alert(&#039;name&#039;)&lt;/script&gt;</div>

<div>{{"<a>Plain text</a>"}}</div>
<div>@&lt;a&gt;Plain text&lt;/a&gt;</div>