Laravel刀片内联部分

时间:2017-07-12 13:13:16

标签: php laravel blade laravel-blade

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

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

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

3 个答案:

答案 0 :(得分:2)

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

第1页:

@stack('small')

第2页:

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

答案 1 :(得分:0)

您需要在Laravel中使用Component并在example.blade.php中创建模板并在组件中设置变量,并在调用时将变量发送到组件,如:

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

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

答案 2 :(得分:0)

你的意思是重复@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