继承布局
继承文件
@extends('layouts.app')
继承节点
@section('content')@endsection
引入子视图
Blade 模板使用 @include 包含另一个视图,子视图将继承父视图的所有可用变量:
@include('admin.user')
组件 & 插槽
实现可复用的组件
- 创建组件
在 views 中创建 components 文件夹,然后在里面写组件代码:
如:创建名为 alert 的组件,路径: views \ components \ alert.blade.php
- 组件起别名
在 App\Providers 文件的 AppServiceProvider 的 boot 方法中起别名:
将 名为 components.lert 的组件 别名命名为 alert
use Illuminate\Support\Facades\Blade; //要引入 Blade 的命名空间Blade::component('components.alert', 'alert');
- 使用组件
注意:代码中的@alert(['type' => 'danger'])You are not allowed to access this resource!@endalert
type是我们自定义的插槽,如果没有自定义,可以省略,如:@alertYou are not allowed to access this resource!@endalert
