视图中 @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 协议》,转载必须注明作者和本文链接
悲观者永远正确,乐观者永远前行。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!