Home > Back-end >  Laravel - Same section used in different layout
Laravel - Same section used in different layout

Time:09-29

I have 2 layouts as shown below:

Layout A

<html>
  <body>
    <section>
      Hello
    </section>
    This is 1st
    @yield('content')
  </body>
</html>

Layout B

<html>
  <head></head>
  <body>
    <section>
      Hello
    </section>
    This is 2nd
    @yield('content')
  </body>
</html>

Layout A and B both have same section. Anything I can do to keep the code DRY?

CodePudding user response:

Option 1

In resources/views/ create a layouts/ directory, and within that create a file called "main.blade.php".

<html>
  <body>
    <section>
    Hello
    </section>
    @yield('title')
    @yield('content')
  </body>
</html>

Then in each of your two views above you would use :

@extends('layouts/invalid')
@section('title')
    This is 1st / This is 2nd (as the case may be)
@stop
@section('content')
    Content goes here
@stop

Option 2

Use includes - in resources/views/ create an includes/ directory, and within that create section.blade.php with the content :

<section>
  Hello
</section>

Then in your two views above :

<html>
  <body>
    @include('includes.section')
    This is 1st / This is 2nd (as the case may be)
  </body>
</html>
  • Related