mstnorris's avatar

Extending Multiple Blade Hierarchy

I would like to know if it is possible to accomplish the following.

I have the following folder structure

├── articles
│   ├── _form.blade.php
│   ├── create.blade.php
│   ├── edit.blade.php
│   ├── index.blade.php
│   ├── master.blade.php
│   └── show.blade.php
├── auth
│   ├── login.blade.php
│   ├── password.blade.php
│   ├── register.blade.php
│   └── reset.blade.php
├── layouts
│   ├── app.blade.php
│   └── master.blade.php
└── welcome.blade.php

layouts/master.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Whiteboard</title>
    <link rel="stylesheet" href="/css/app.css" type="text/css" media="screen" />
</head>
<body>

    <div class="container-fluid">

        @yield('tl_content')

    </div>

    @yield('footer')

</body>
</html>

articles/master.blade.php

@extends('layouts.master')

@section('tl_content')

<div class="row">

    <div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-12">

        @yield('content')

    </div>

</div>

@stop

articles/index.blade.php

@extends('articles.master')

@section('content')

<h1>Articles</h1>

    <hr>

    @foreach ( $articles as $article )

        <article>

            <h2>
                <a href="{{ action('ArticlesController@show', [$article->id]) }}">{{ $article->title }}</a>
            </h2>

            <div class="body">{{ $article->body }}</div>

        </article>

    @endforeach

@stop
0 likes
7 replies
RachidLaasri's avatar

I have never tried something like that, but i think it's possible, or you can do an include.

usman's avatar

Yes, it is possible I am currently using this pattern in one of my projects.

Usman.

mstnorris's avatar

I don't think I can include as it won't always be reference on that level.

Some views are rendered like so (extending multiple layouts)

layouts.master.blade.php
                   |
articles.master.blade.php
                   |
articles.index.blade.php

Others will just extend layouts.master

layouts.master.blade.php
                   |
pages.about.blade.php
usman's avatar

Here are the relevant portions from my markup

//resources/views/layouts/master.blade.php
<body>
    <div class="header">
        @include('partials.navigation')
    </div>
    <div class="content-main">
        @yield('header')
        <div class="container">
            @yield('main')
        </div>
    </div>
    <div class="footer">

//rerouces/views/index.blade.php
@extends('layouts.master')
@section('main')
    <div class="row">
        <div class="col-md-8 content">
            @if(! app('request')->has('s'))
                @include('partials.tabs')
            @else
                <h1 class="page-header">Showing results for: {{app('request')->input('s')}}</h2>
            @endif
            @yield('content')
        </div>
......
//resources/views/link/list.blade.php
@section('content')
    @if(! $links->isEmpty())
        <div id="index" class="add-content">
            @foreach($links as $link)
                @include('link.link')
            @endforeach
        </div>
        @if( ! app('request')->ajax())
....
usman's avatar

Are you getting errors, how are you making the view inside your controllers?

Please or to participate in this conversation.