Laravel blade 内联部分

Laravel blade inline section

我需要在我的 blade 模板中复制一段 html,但它太小了,我不想创建新文件。我可以在我使用的同一个文件中创建一个部分吗?例如:

@section('small')
    <div>small piece of html</div>
@endsection

@include('small')
<div>some html here</div>
@include('small')

您需要将 Component 与 Laravel 一起使用,并在 example.blade.php 中制作模板并在组件中设置变量,然后将变量发送到组件,同时调用它:

@component('alert', ['foo' => 'bar'])
    ...
@endcomponent

所以它在不同的文件中,但是在将变量发送到组件时,它可以是高效的。

你的意思是重复@section?如果是,那么用另一个名字创建@yield,并在同一个模板中调用这两个部分。

第 1 页:

@yield('section1')
@yield('section2')

第 2 页:

@section('section1')
    <div>small piece of html</div>
@endsection

@section('section2')
     <div>some html here</div>
@endsection

我认为 @stack@push 是您需要的,而不是 @section 假设您要在其他文件上使用它

第 1 页:

@stack('small')

第 2 页:

@push('small')
    <div>small piece of html</div>
@endpush
@push('small')
     <div>some html here</div>
@endpush