Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use directive @push in blade template laravel

Tags:

I need script only on one page. and it should load after jQuery. I tried in index.blade

<script type="text/javascript" src="{{ URL::asset ('js/jquery.js') }}"></script> @push('custom-scripts')     <script type="text/javascript" src="{{ URL::asset ('js/custom-scripts.js') }}"></script> @endpush 

and then I should use @stack('custom-scripts') in my view?

like image 397
kuka Avatar asked Jun 21 '17 11:06

kuka


People also ask

Can we write PHP code in blade template Laravel?

Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates. In fact, all Blade templates are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.

Where are Laravel blade directives?

Open your laravel project on phpStorm and go to any view and type @ and hit control + space , you will see a list of directives that you can use.


1 Answers

You just need to make that the opposite way, @push is meant to be in the child view, which is pushing content to the parent @stack directive.

So your index.blade.php should have a:

@stack('custom-scripts') 

And your child view:

@push('custom-scripts')     <script type="text/javascript" src="{{ URL::asset ('js/custom-scripts.js') }}"></script> @endpush 

You can also use @parent directive with @section like this:

//index.blade.php @yield('scripts')   //child.blade.php @section('scripts')     @parent     <!-- The rest of your scripts --> @endsection 

If you need more info I'd recommend you to check the documentation. Hope this helped you.

like image 57
Asur Avatar answered Sep 19 '22 17:09

Asur