Laravel - 不同布局中使用的相同部分

Laravel - Same section used in different layout

我有 2 个布局,如下所示:

布局 A

<html>
  <body>
    <section>
      Hello
    </section>
    This is 1st
    @yield('content')
  </body>
</html>

布局 B

<html>
  <head></head>
  <body>
    <section>
      Hello
    </section>
    This is 2nd
    @yield('content')
  </body>
</html>

布局 A 和 B 都有相同的部分。 我能做些什么来保持代码干燥?

选项 1

在 resources/views/ 中创建一个 layouts/ 目录,并在其中创建一个名为“main.blade.php”的文件。

<html>
  <body>
    <section>
    Hello
    </section>
    @yield('title')
    @yield('content')
  </body>
</html>

然后在上面的两个视图中,您将使用:

@extends('layouts/main')
@section('title')
    This is 1st / This is 2nd (as the case may be)
@stop
@section('content')
    Content goes here
@stop

选项 2

使用 includes - 在 resources/views/ 创建一个 includes/ 目录,并在其中创建 section.blade.php 内容为:

<section>
  Hello
</section>

那么在你上面的两个视图中:

<html>
  <body>
    @include('includes.section')
    This is 1st / This is 2nd (as the case may be)
  </body>
</html>