Laravel blade append to section not working properly

2.8k Views Asked by At

I want to write to a region in my template from the view and also from an included view thing the first view but no matter what I try it doesn't work. I have tried using @parent and @append but nothing has produced the result that I want so far.

In my layout I have the following:

@yield('scripts')

In my view primary I have the following:

@include('admin/rules/partials/_form')

@section('scripts)
    // javascript #1 here
@stop

In admin/rules/partials/_form I have the following:

@section('scripts)
    // javascript #2 here
@stop

So like I said I have tried using @parent after @section('scripts') and also using @append instead of @stop but nothing I do includes the two script blocks properly. The best I've had so far is javascript #1 being included once and javascript #2 being included twice. How would I do this so that each block of code is appended to the scripts region only once?

3

There are 3 best solutions below

0
On BEST ANSWER

I solved it in the end I had to do the following:

@yield('scripts')

In my view primary I have the following:

@include('admin/rules/partials/_form')

@section('scripts')
    // javascript #1 here
@stop

In admin/rules/partials/_form I have the following:

@section('scripts')
    @parent
    // javascript #2 here
@overwrite
2
On

This answer worked for me.

EDIT, but now it doesn't.

EDIT, I updated the code below to work. I was yielding the javascript section on the page and the layout.

tests.layout.blade.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>
            @yield('title') - test
        </title>
        @yield('head')
    </head>
    <body>

    @yield('content')

    @yield('javascript')
    </body>
</html>

tests.page.blade.php

@extends('layouts.default')
@section('title', 'Poses')

@section('content')
    <div class="container">
        <div class="row">
            @include('tests.partials.one')
            @include('tests.partials.two')
        </div>
    </div>
@stop

tests.partials.one.blade.php

@section('content')
    @parent
    <h1>One</h1>
@stop

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

tests.partials.two.blade.php

@section('content')
    @parent
    <h1>Two</h1>
@stop

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

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

0
On

In my case, I'm not set the section @stop. After using @stop, it works properly.