Ad

How To Correct Laravel's Blade Variable Section Scoping When Yielding Inside A Loop?

- 1 answer

When I include a blade template that extends a base blade, any variables within the section of the included blade show only the variables of the first iteration.

Reading around it seems the render order here is important, and views are rendered before variables, or vice versa.

Note

  • I have read this SO question/answer: Laravel Blade @yield variable scope
  • The below snippet is greatly reduced in complexity, so the example could be restructured to exclude sections/extends. However my real case can't be

Example

// index.blade.php
//
@foreach($jobs as $job)
    {{ $job->id }} // <-- Correct output, 1,2,3,..N
    @include('job-detail', ['id' => $job->id])
@endforeach

Then in the job detail blade

// job-detail.blade.php
//
@extends('job-base')

A: {{ $id }} // <-- Correct output, 1,2,3,..N

@section('content')
    B: {{ $id }} // <-- Incorrect output, 1,1,1,..1 (or whatever the first index is)
@endsection // have also tried @stop

Then in the job base blade

// job-base.blade.php
//
@yield('content') // Have also tried @section('content') + @show
Ad

Answer

After wading through the source code, namely BladeCompiler and View/Factory I noticed the following snippet:

protected function compileOverwrite($expression)
{
    return '<?php $__env->stopSection(true); ?>';
}

In the background Laravel appears to store rendered content (by including the file, and extract current variables in a ob_style fashion) in a keyed array, with the view name being the key.

When stopSection is not passed a boolean true, it creates a new key, and the view gets the data from the original key.

Long story short, it's now undocumented (for 5.1+) but can be found in the docs for 5.0: https://laravel.com/docs/5.0/templates

However it doesn't really explain the "why". This page seems to explain it a little better:

http://laravel-recipes.com/recipes/244/stopping-injecting-content-into-a-section-and-overwriting

Ad
source: stackoverflow.com
Ad