Blade: how to include a section in one page but not in another

- 1 answer

Ad

I have a report that I present on screen.

report.blade.php

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

report_sub1.blade.php

@extends('reports.report')

@section('content')
    <h1> This is sub section 1 </h1>

    <h1> This is sub section 2 </h1>
@stop

I want to include the section 2 in the 1st report but not in the second. Meaning I have two identical reports, but the one should not print section 2.

How can you do that?

Ad

Answer

Ad

You could use your report.blade.php as a master page. Then use two partials as sub-sections, and use a third partial for section 1. Finally include this last partial only where you need it:

section1.php

<!-- section 1 definition-->
<h1> This is sub section 1 </h1>   

report_sub1.blade.php

@extends('reports.report')

@section('content')
     <!-- section 1 included -->
     @include('section1')

     <!-- section 2 included only here--> 
     <h1> This is sub section 2 </h1>  
@stop

report_sub2.blade.php

@extends('reports.report')

@section('content')
     <!-- section 1 included -->
    @include('section1') 

    <!-- section 2 included is not included here-->   
@stop
Ad
source: stackoverflow.com
Ad