Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4: How to register a script that goes with a partial view so that it loads after its javascript dependencies?

Have a Laravel 4 web site that uses a layout where jquery, boostrap, etc are loaded at the bottom of the page:

<html>
   <body>

      <!-- stuff -->

      <script src="...jquery, bootstrap, etc..."></script>
   </body>
</html>

In some of the pages on the site I'm also using a partial view, and the view uses a script that depends on the bottom-of-the-page scripts. To keep things clean I'd like for the script to be part of the view file itself so that whenever the view is included its corresponding javascript is included as well. However, when the script is part of the view, it fails because the script tags will come before the dependencies at the bottom of the page.

Is there any nice way using something like blade's @yield and @section directives to keep the script in the same file as the view, but have it load in a section of the layout below the javascript dependencies? I don't believe I can use @yield/@section because the partial does not @extend any layout. It is simply @included in the views that need it.

So what I'd like in the end is something like:

partial.blade.php:

<div><!-- partial's html stuff --></div>
<script><!-- partial's script stuff --></div>

view.blade.php:

@extends('layout')

@section('content')
   @include('partial')
@stop

layout.blade.php

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

      <script><!-- jquery, bootstrap, etc. --></script>

   </body>
</html>

final html output:

<html>
   <body>

      <div><!-- partial's html stuff --></div>

      <script src="...jquery, bootstrap, etc..."></script>

      <script><!-- partial's script stuff --></div>
   </body>
</html>

Obviously, the easy answer is to load the dependencies in the <head> tag, but I'd like to know if it's possible to avoid this.

like image 634
ralbatross Avatar asked Oct 14 '25 07:10

ralbatross


2 Answers

You can try this. I've tested it and i believe it works.

LAYOUT

<html>

   <body>

        @yield('content')   

        <script>'Scripts From Layout'</script>

        @yield('scripts')

   </body>

</html>

VIEW

@extends('layout')

@section('content')

   @include('partial')

@stop

@section('scripts')

   @include('partial')

@stop

PARTIAL

@section('content')

    <div>DIV From Partial</div>

@stop

@section('scripts')

    <script>'Scripts From Partial'</script>

@stop

OUTPUT

<html>

   <body>

    <div>DIV from partial</div>

    <script>'Scripts From Layout'</script>

    <script>'Script From Partial'</script>

   </body>

</html>
like image 147
tliokos Avatar answered Oct 19 '25 13:10

tliokos


Define a section called 'scripts' in your layout that comes after all necessary JS libraries have been loaded.

<script src="..."></script>
@yield('scripts')

Your views can use @section('scripts') to put inline JS into the section.

like image 35
Andreas Avatar answered Oct 19 '25 15:10

Andreas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!