Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Blade - Chain/Embed multiple layouts

In my favorite templating frameworks they typically have the ability to nest layouts. Is this something that is possible in Blade?

For example...

master.blade.php

<html>
  <head><!-- stuff --></head>
  <body>
    @yield('content')
  </body>
</html>

nav.blade.php

@extend('master')
<nav>
    <!-- nav content -->
</nav>
@yeild('content')

breadcrumb.blade.php

@extend('nav')
<breadcrumb>
    <!-- breadcrumb content -->
</breadcrumb>
@yield('content')

home.blade.php

@extend('nav')
@section('content')
    <home>
        <!-- content -->
    </home>
@endsection

about.blade.php

@extend('breadcrumb')
@section('content')
    <about>
        <!-- content -->
    </about>
@endsection

The reason I love this format is that it makes it extremely elegant (IMO) to be able to choose your injection point!

  • Have a one off landing page...reference master
  • For the homepage...reference nav
  • For any subpages (about/nav/product) reference breadcrumb

The layouts cascade and 'content' gets rebuilt with the compiled html as it goes up the tree.

Is this possible? I'm hoping to avoid doing @include in the layouts as I personally find them cumbersome and a bit of an eye sore especially when you get to elements that are repeated often, but not everywhere (breadcrumbs).

EDIT: Based on answers.

Ideally 'content' would be rebuilt and passed up the chain of nested layouts. i.e. If you have the homepage which references nav.blade.php the homepage content gets added to the nav layout and compiled. Then since the nav layout references master.blade.php the compiled layout would be passed up to master and built again. No duplicating of any content.

like image 805
Jared Avatar asked May 25 '17 15:05

Jared


1 Answers

I'm not sure I get what you're after here. For instance in home.blade.php you extend "nav", which in turn extends "master", but both "master" and "nav" yield content, so the <home> content will render twice.

So, what is your expected output? I'm not sure "home" or "about" should really extend "nav" or "breadcrumb". I think of these two as sort of structural layout components, so it does make sense to me to include them in the master layout. In "nav" you can define a section to extend when your view needs a breadcrumb.

For instance:

master.blade.php

<html>
<head><!-- stuff --></head>
  <body>
    @include('nav')        
    @yield('content')
  </body>
</html>

nav.blade.php

<nav>
  <!-- nav content -->
  @yield('breadcrumb')
</nav>

home.blade.php

@extend('master')

@section('content')
  <home>
    <!-- content -->
  </home>
@endsection

about.blade.php

@extend('master')

@section('breadcrumb')
  <breadcrumb>
    <!-- breadcrumb content -->
  </breadcrumb>
@endsection

@section('content')
  <about>
    <!-- content -->
  </about>
@endsection
like image 178
alepeino Avatar answered Sep 19 '22 09:09

alepeino