Laravel刀片@include与@yield

Jam*_*mer 2 php laravel blade laravel-blade

在Laravel刀片上有类似的东西吗?

Route::get('blade', function () {
    return view('aMainPage');
});
Run Code Online (Sandbox Code Playgroud)

aMainPage.blade.php

@include('molecules.blocks.banner', ['background'=> '/image.jpeg'])
   <h1>I am included</h1>
@endinclude
Run Code Online (Sandbox Code Playgroud)

/views/molecules/blocks/banner.blade.php

@if($background)
<section class="banner" style="background-image:url('{{$background}}');">
@else
<section class="banner">
@endif
    <h1>Hello</h1>
    @yield() {{- notice @yield here -}}
</section>
Run Code Online (Sandbox Code Playgroud)

期望的输出

<section class="banner" style="background-image:url('/image.jpeg');">
    <h1>Hello</h1>
    <h1>I am included</h1>
</section>
Run Code Online (Sandbox Code Playgroud)

这是上面呈现的内容

实际产出

 <section class="banner" style="background-image:url('/image.jpeg');">
    <h1>Hello</h1>
</section>


    <h1>I am included</h1>
@endinclude
Run Code Online (Sandbox Code Playgroud)

xde*_*ull 5

当然可能,

分子/块/ banner.blade.php

@if(isset($background))
  <section class="banner" style="background-image:url('{{$background}}');">
@else
  <section class="banner">
@endif
      <h1>Hi</h1>
      @yield('main_content')
</section>
Run Code Online (Sandbox Code Playgroud)

aMainPage.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vNull</title>
</head>
<body>
@section('main_content')
    <h1>Woho</h1>
@endsection
@include('molecules.blocks.banner', ['background' => '/image.jpeg'])
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

二次解决方案

分子/块/ banner.blade.php

@if(isset($background))
  <section class="banner" style="background-image:url('{{$background}}');">
@else
  <section class="banner">
@endif
      <h1>Hi</h1>
      @stack('main_content')
</section>
Run Code Online (Sandbox Code Playgroud)

aMainPage.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vNull</title>
</head>
<body>
@include('molecules.blocks.banner', ['background' => '/image.jpeg'])
@push('main_content')
    <h1>Woho</h1>
@endpush
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

结果

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vNull</title>
</head>
<body>
  <section class="banner" style="background-image:url('/image.jpeg');">
      <h1>Hello</h1>
      <h1>Woho</h1>
  </section>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)