Ad

Child Partial Not Injecting Styles Into Its Parent Blade Layout

I have one partial child view that requires some custom js and styles. This child is the only view in the entire application that uses it. It's complex and so the JS was broken off into its own file.

I have tried to 'inject' the script and style into the parent layout according to the docs and it doesn't seem to be working. I have tired a combination of @parent and @@parent in the sections. I have tried changing the main layouts, @yield('scripts')@yield('styles') to @section('scripts')@show.

What am I missing? Is this even possible?

main.blade.php (Shortened For Brevity)

<head>
    @include('apps.includes.styles')

    @yield('styles')//a place for custom style
</head>
<body>
@yield('content')

@include('apps.includes.scripts')
@yield('scripts') //a place for custom scripts
</body>

create.blade.php

@extends('layouts.main')
@section('styles')@endsection
@section('scripts')@endsection
@section('content')
    <div>
      @include('partials._toolbar')
    </div>
@endsection

_toolbar.blade.php

@section('styles')
    @parent
    <script src="{{asset('assets/css/mystyle.css')}}"></script>
@endsection

@section('scripts')
    @parent
    <script src="{{asset('assets/js/myjs.js')}}"></script>
@endsection

<section>
// my toolbar html code
</section>
Ad

Answer

What you are trying to do is not possible. You can fill sections only by extending a layout, not by including it. Instead, use a stack for your styles:

toolbar.blade.php

@push('styles')
    <script src="{{asset('assets/css/mystyle.css')}}"></script>
@endpush

main.blade.php

<head>
    @include('apps.includes.styles')

    @stack('styles')
</head>

You can read more about stacks in the official documentation.

Ad
source: stackoverflow.com
Ad