jeudi 21 janvier 2016

How to correct Laravel's Blade variable section scoping when yielding inside a loop?

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire