继承布局

  1. 继承文件

    1. @extends('layouts.app')
  2. 继承节点

    1. @section('content')
    2. @endsection

引入子视图

Blade 模板使用 @include 包含另一个视图,子视图将继承父视图的所有可用变量:

  1. @include('admin.user')

组件 & 插槽

实现可复用的组件

  1. 创建组件

views 中创建 components 文件夹,然后在里面写组件代码:
如:创建名为 alert 的组件,路径: views \ components \ alert.blade.php

  1. 组件起别名

App\Providers 文件的 AppServiceProviderboot 方法中起别名:
将 名为 components.lert 的组件 别名命名为 alert

  1. use Illuminate\Support\Facades\Blade; //要引入 Blade 的命名空间
  2. Blade::component('components.alert', 'alert');
  1. 使用组件
    1. @alert(['type' => 'danger'])
    2. You are not allowed to access this resource!
    3. @endalert
    注意:代码中的 type 是我们自定义的插槽,如果没有自定义,可以省略,如:
    1. @alert
    2. You are not allowed to access this resource!
    3. @endalert