M
M
Mo1she2018-11-28 06:41:36
Laravel
Mo1she, 2018-11-28 06:41:36

Connecting laravel scripts?

In the general app.blade php template, I have a construct - Then I extend this template in the news.blade.php template and, as usual, I declare the script section -
@yield('scripts')

@section('scripts')
    //скрипты, относящиеся к news.blade.php
@endsection

Also in the same template (news.blade.php) I include the sidebar.blade.php file, and in this file I again want to include scripts related only to this file (sidebar.blade.php) -
@section('scripts')
    //скрипты, относящиеся к sidebar.blade.php
@endsection

That is, in the end, I want the @yield('scripts') construct in the app.blade.php template to include the contents of two scripts sections at once. What is the best way to implement this? I know that you can make two sections scripts-1 and scripts-2, but each file has its own scripts and if you create this for each file, it will turn out ugly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2018-11-28
@Mo1she

Stacks
Blade allows you to push to named stacks which can be rendered somewhere else in another view or layout. This can be particularly useful for specifying any JavaScript libraries required by your child views:
@push('scripts')
    <script src="/example.js"></script>
@endpush

You may push to a stack as many times as needed. To render the complete stack contents, pass the name of the stack to the @stackdirective:
<head>
    <!-- Head Contents -->

    @stack('scripts')
</head>

https://laravel.com/docs/5.7/blade#stacks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question