Blade @stack @push

Have you ever wonder if there are any other way than always, in every blade child view section adding @parent?

@section('javascript')

@parent

<script> 
    ...
</script>

@endsection

and if you forget to add @parent in the section, the master template @yield are override.

Solution is to use blade @stack and @push.

If in your master template you have

/* layouts/app.blade.php */
@stack('javascript')

in your child views you can make as many pushes as you like

/* blog/index.blade.php */
@push('javascript')
<script>
    // blog based javascript
</script>
@endpush

/* blog/post.blade.php */
@push('javascript')
<script>
    // post based javascript
</script>
@endpush

/* final rendered html in the browser */
<script>
    // blog based javascript
</script>

<script>
    // post based javascript
</script>

I do not know why it is not still in API documentation.

But you can find it in Illuminate/View/Compilers/BladeCompiler of your Laravel 5.2 project.

 /**
  * Compile the push statements into valid PHP.
  *
  * @param  string  $expression
  * @return string
  */
 protected function compilePush($expression)
 {
     return "<?php \$__env->startSection{$expression}; ?>";
 }
Share on FacebookShare on RedditShare on Google+Tweet about this on TwitterShare on LinkedIn
don't waste your time using copy/paste, just use share buttons above
Blade @stack @push