【问题标题】:Laravel blade inline sectionLaravel 刀片内联部分
【发布时间】:2017-07-13 10:07:52
【问题描述】:
我需要在刀片模板中复制一段 html,但它太小了,我不想创建新文件。我可以在使用它的同一个文件中创建一个部分吗?例如:
@section('small')
<div>small piece of html</div>
@endsection
@include('small')
<div>some html here</div>
@include('small')
【问题讨论】:
-
也许您想在这里创建Component? @component('small') @endcomponent
-
标签:
php
laravel
blade
laravel-blade
【解决方案1】:
我认为 @stack 和 @push 是您需要的,而不是 @section
假设您要在其他文件上使用它
第 1 页:
@stack('small')
第 2 页:
@push('small')
<div>small piece of html</div>
@endpush
@push('small')
<div>some html here</div>
@endpush
【解决方案2】:
你需要在 Laravel 中使用Component 并在 example.blade.php 中创建你的模板,并在组件中设置你的变量,并在调用它时将变量发送到组件:
@component('alert', ['foo' => 'bar'])
...
@endcomponent
所以它在不同的文件中,但是在将变量发送到组件时,它可以是高效的。
【解决方案3】:
你的意思是重复的@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