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



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

布局

<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都有相同的section。我能做些什么来保持代码干燥?

选项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>

最新更新