Partial views with JavaScript in Laravel 5

前端 未结 1 1957
梦如初夏
梦如初夏 2021-01-01 02:49

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 inclu

相关标签:
1条回答
  • 2021-01-01 03:16

    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

    0 讨论(0)
提交回复
热议问题