Ryanthehouse Ryanthehouse - 2 months ago 17
PHP Question

Laravel Blade | Load Content to Parent via Included View

Let's say I have the following Master layout:

//Master Layout
//--------------------------
<head>
@section('styles')
//Some Master Styles
@show
</head>
<body>
//Header
@section('header')
@include('header')
@show

//Content
@section('content')
//Content for page is extending this view
@show

//Footer
@section('footer')
@include('footer')
@show
</body>


In my Page, I would do something like:

//Page
//--------------------------
@extends('master')

@section('styles')
@parent
//Page Stylesheet
@endsection


This would add the page specific style to the of the Master Layout. This same technique does not seem to work for Views that do not extend a layout and are, rather, @included in the layout.

How to I add stylesheets in this same manner from the @included Footer, for example?

Here is what I tried that did not work as expected:

//Footer
//--------------------------

//Add Footer Stylesheet
@section('styles')
@parent
// Stylesheet <link>
@show
<div>
//Footer Content
</div>


I cannot seem to find any example of this. It seems as though the fact that Page extends Master is the only reason it can load its styles to the layout . A View that is simply @included does not allow for the same result.

Answer

You don't need to do

@extends('master')

@section('styles')
    @parent
    {{-- Page Stylesheet --}}
@endsection

in your respective pages so as to load page specific stylesheets.

You should rather load page specific stylesheets for your master.blade.php file so as to keep your code dry.

To to that, you shout specify the route or expected url format of such pages, then, the appropriate stylesheet(s) to be loaded.

You can do that this way in your master.blade.php file:

@section('styles')
    @if(Request::is('transactions/generate-invoice'))
        @include('generate-invoice-css')
    @elseif(Request::is('transactions/users'))
        @include('users-css')
    @endif
@show

Where generate-invoice-css.blade.php contains the stylesheet(s) you want loaded for the page content accessible at yoursite.com/transactions/generate-invoice and users-css.blade.php, that of yoursite.com/transactions/users.

For a given pattern as in: same stylesheets for pages under transactions, you can do this:

@if(Request::is('transactions*'))

using a wildcard *.

To load a given resource to a location other than the <head> section of your pages, simply use the same approach and adapt as appropriate.

To load page specific resources with an @include() from your master.blade.php, use this approach (in your master.blade.php file):

@section('styles')
    @include('styles')
@show

where styles.blade.php should contain those your conditions for the appropriate resources to be loaded satisfying your requirement(s) for the purpose as in:

@if(Request::is('transactions/generate-invoice'))
    @include('generate-invoice-css')
@elseif(Request::is('transactions/users'))
    @include('users-css')
@endif

As the content of your styles.blade.php.