Laravel blade 扩展两种不同布局的模板

Laravel blade template extending two different layouts

我有两个几乎相同的模板。这是邮件模板,但它应该在两个不同的地方。所以他们扩展了两个不同的模板,但是这和部分名称是唯一的区别。

示例如下:

@extends('layout.client')
    //sections etc
@section('content')
    //content here
@endsection
    //other sections etc

这是第二个模板:

@extends('layout.company')
    //sections etc
@section('contentinner')
    //content here
@endsection
    //other sections etc

其他部分大多是导入的库,以使内容有效。

我应该如何处理才能不重复代码?

创建另一个模板并包含在您的部分中

<div class="col-md-3">
  @include('layouts.your_template')
</div>

我使用 components 解决了类似的问题。

我发现有一个主布局很好,但是如果您有部分代码需要包含在多个页面中,最好使用组件。 This 提供了一个很好的组件使用示例。

将您的模板放入components.yourtemplate.blade.php

然后在 layout.clientlayout.company 中都输入

@component('components.yourtemplate')
@endcomponent

您希望包含模板代码的位置。您可以在多个视图中包含相同的组件。