Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  37] [ 7]  / answers: 1 / hits: 9110  / 10 Years ago, wed, march 12, 2014, 12:00:00

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.


More From » uiview

 Answers
6

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>

[#46912] Wednesday, March 12, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amberlykaliac

Total Points: 415
Total Questions: 100
Total Answers: 85

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
amberlykaliac questions
Mon, Sep 28, 20, 00:00, 4 Years ago
Wed, Jul 29, 20, 00:00, 4 Years ago
Sun, Jul 12, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;