Laravel刀片@include与@yield

时间:2016-04-05 21:58:10

标签: php laravel blade laravel-blade

Laravel刀片中是否有类似的东西?

Route::get('blade', function () {
    return view('aMainPage');
});

aMainPage.blade.php

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

/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>

所需输出

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

以上是目前呈现的内容

实际输出

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


    <h1>I am included</h1>
@endinclude

1 个答案:

答案 0 :(得分: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>

<强> 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>
  

二级解决方案

<强>分子/块/ 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>

<强> 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>

<强>结果

<!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>