Laravel Blade:扩展多个视图并添加更多内容

时间:2017-09-11 19:44:17

标签: php laravel laravel-5 blade laravel-blade

我有以下场景:一个显示一些数据的视图(让我们称之为“汽车”),另一个显示其他数据的视图(让我们称之为“摩托车”)和一个必须显示“汽车”数据的视图“和”摩托车“除了一些额外的内容(让我们称之为”车辆“)。 这三种观点本身都有意义,可以单独展示。

问题是我无法找到构建“车辆”视图而不重复代码的方法,因为我不知道如何使用Laravel的Blade模板来扩展多个视图('汽车'和'摩托车')并添加更多内容。每个视图分为三个部分('styles','scripts'和'content'),因为每个视图的HTML内容都需要特定的CSS和JS文件。

layouts / master.blade.php(简化版)

<html>
<head>
    @yield('styles')
    @yield('scripts')
</head>
<body>
    @yield('content')
</body>
</html>

cars.blade.php(简化版)

@extends('layouts.master')

@section('styles')
    [CSS files required by the HTML content that shows cars]
@stop
@section('scripts')
    [JS files required by the HTML content that shows cars]
@stop
@section('content')
    [HTML content that shows cars]
@stop

motorbikes.blade.php(简化版)

@extends('layouts.master')

@section('styles')
    [CSS files required by the HTML content that shows motorbikes]
@stop
@section('scripts')
    [JS files required by the HTML content that shows motorbikes]
@stop
@section('content')
    [HTML content that shows motorbikes]
@stop

vehicles.blade.php(简化版)

@extends('?') <!-- It should extend 'cars' and 'motorbikes' -->

@section('styles')
    @parent <!-- It should refer to the content of 'cars' and 'motorbikes' -->
    [CSS files required by the HTML content that shows aditional info about vehicles]
@stop
@section('scripts')
    @parent <!-- It should refer to the content of 'cars' and 'motorbikes' -->
    [JS files required by the HTML content that shows aditional info about vehicles]
@stop
@section('content')
    @parent <!-- It should refer to the content of 'cars' and 'motorbikes' -->
    [HTML content that shows aditional info about vehicles]
@stop

不受欢迎的方法

我已经考虑将视图'cars'和视图'motorbikes'拆分为以下文件:

cars/styles.blade.php
cars/scripts.blade.php
cars/content.blade.php
motorbikes/styles.blade.php
motorbikes/scripts.blade.php
motorbikes/content.blade.php

然后我可以像这样构建vehicles.blade.php:

@extends('layouts.master')

@section('styles')
    @include('cars.styles')
    @include('motorbikes.styles')
    [CSS files required by the HTML content that shows aditional info about vehicles]
@stop
@section('scripts')
    @include('cars.scripts')
    @include('motorbikes.scripts')
    [JS files required by the HTML content that shows aditional info about vehicles]
@stop
@section('content')
    @include('cars.content')
    @include('motorbikes.content')
    [HTML content that shows aditional info about vehicles]
@stop

但我想避免这种分裂,因为事实上,我有更多的观点,而不仅仅是'汽车'和'摩托车',我觉得不希望将文件数量乘以3。

欢迎提出建议。

0 个答案:

没有答案