Partial views with JavaScript in Laravel 5

I have multiple partial blade files that all require just a little bit of JavaScript. Ideally I’d like the partial to load the JS it needs and the normal JS wouldn’t include it. I know you can load it all into one big JS file, but I am wondering if there is a way to have a more elegant solution.

I’d like the solution below to work for multiple files (it works for one, but not n. The first partial blade encounters is used due to blade’s rendering engine).

  • JavaScript click function table cell only applied on the first row
  • Call to undefined method Illuminate\Database\Query\Builder::attach() In laravel 5.3
  • Using Laravel functions inside a javascript file that is included as an asset
  • Laravel 4: How to register a script that goes with a partial view so that it loads after its javascript dependencies?
  • Laravel 4 blade syntax within my javascript files
  • How to pass PHP variable to javascript function from blade format html
  • test.page.blade.php

    @extends('layouts.default')
    @section('title', 'Poses')
    
    @section('content')
        <div class="container">
            <div class="row">
                @include('test.partial.one')
                @include('test.partial.two')
            </div>
        </div>
    @stop
    @section('javascript')
        @include('test.partial.one')
        @include('test.partial.two')
    @stop
    

    partial test.partial.one.blade.php

    @section('content')
    
        <h1>One</h1>
    
    @stop
    
    @section('javascript')
    
        <script>Scripts from one</script>
    
    @stop
    

    partial test.partial.two.blade.php

    @section('content')
    
        <h1>Two</h1>
    
    @stop
    
    @section('javascript')
    
        <script>Scripts from two</script>
    
    @stop
    

  • How to get name of an option value when validation returns false in laravel 5
  • Using Laravel functions inside a javascript file that is included as an asset
  • Laravel 4: How to register a script that goes with a partial view so that it loads after its javascript dependencies?
  • Laravel 4 blade syntax within my javascript files
  • Call to undefined method Illuminate\Database\Query\Builder::attach() In laravel 5.3
  • How to pass PHP variable to javascript function from blade format html
  • One Solution collect form web for “Partial views with JavaScript in Laravel 5”

    The solution to your problem is the @parent directive, which you have to use in your javascript sections.

    test.page.blade.php

    @extends('layouts.default')
    @section('title', 'Poses')
    
    @section('content')
        <div class="container">
            <div class="row">
                @include('test.partial.one')
                @include('test.partial.two')
            </div>
        </div>
    @stop
    
    @yield('javascript')
    

    test.partial.one.blade.php

    @section('content')
        <h1>One</h1>
    @stop
    
    @section('javascript')
        <script>Scripts from one</script>
        @parent
    @stop
    

    test.partial.two.blade.php

    @section('content')
        <h1>Two</h1>
    @stop
    
    @section('javascript')
        <script>Scripts from two</script>
        @parent
    @stop
    

    For further reference: http://laravel.com/docs/5.0/templates