套用布局模板@yield与@section

公用的内容

<!-- 保存在  resources/views/layouts/app.blade.php 文件中 -->

<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

具体内容

<!-- 保存在 resources/views/child.blade.php 中 -->
{{--引用上方的文件,真正加载的时候只需要加载这里就好--}}
@extends('layouts.app')

@section('title', 'Page Title')

@section('sidebar')
    @parent

    <p>This is appended to the master sidebar.</p>
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection

运行结果

This is the master sidebar.
This is appended to the master sidebar.

This is my body content.

公用文件用@section则必须使用@show才能显示

@yield不具体显示内容,具体显示的内容由@section来定义,括号内的字符来联系@yield与@section。

@section有两种书写方式:
1.@section('@yield括号内的字符','具体内容'),

@section('@yield括号内的字符')
  具体内容
@endsection

公用文件使用了@section,具体文件也用了同一个@section,用@parent 不覆盖内容

一般@yield写在公用文件内

@section写在具体文件内,是@yield的实例化

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容