lundi 11 septembre 2017

Laravel Blade: extend multiple views and adding more content

I have the following scenario: a view that shows some data (let's call it "cars"), another view that shows some other data (let's call it "motorbikes") and a view that must show the data of "cars" and "motorbikes" in addition of some extra content (let's call it "vehicles"). The three views have sense by themselves and can be shown separatelly.

The problem is that I can't find the way to build the "vehicles" view without repeating code because I don't know how to use Laravel's Blade templating in order to extend multiple views ('cars' and 'motorbikes') and add more content. Each view is divided into three sections ('styles', 'scripts' and 'content') because the HTML content of each view needs specific CSS and JS files.

layouts/master.blade.php (simplified version)

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

cars.blade.php (simplified version)

@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 (simplified version)

@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 (simplified version)

@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

Undesirable approach

I have already thought about splitting the view 'cars' and the view 'motorbikes' into separated files like:

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

Then I could build vehicles.blade.php like this:

@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

But I want to avoid this kind of splitting because, in fact, I have many more views than just 'cars' and 'motorbikes' and I find undesirable to multiply the number of files by three.

Suggestions are welcome.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire