视图中 @section / @show 和 @section / @endsection 的区别
Laravel 视图中可以使用 @section
来实现父视图定义的内容区块,我们知道 @yield
可以定义子视图需要实现的区块。
那么 @section / @show
和 @section / @endsection
的区别是什么呢?
// parent.blade.php
<html>
<head>
<title>父视图</title>
</head>
<body>
<div>
@yield('test1')
</div>
<div>
@section('test2')
<h3>
我是 H3 号标题
</h3>
@show
</div>
</body>
</html>
// child.blade.php
@extends("parent")
@section('test1')
区块1
@endsection
@section('test2')
@parent
<div>
我是文章内容
</div>
</div>
@endsection
渲染结果
可以看见,在 @section('test2')
中可以使用 @parent
来访问父视图该区域的默认内容,在我们的子视图继承父视图并需要访问父视图渲染区域的默认内容时,可以在父视图中使用 @section / @show
来定义内容区块,而非使用 @yield
。
本作品采用《CC 协议》,转载必须注明作者和本文链接