vendredi 25 septembre 2020

Laravel Blade, compile all @includes before checking for variables or pass foreach variable without error

This question is similar to my previous question (Laravel Blade, Pass a function through @include without checking if it exists in main view), but the solution there doesn't work this time.

I am compiling multiple similar views using @includes to reuse code. Sometimes the @includes are within foreach loops, sometimes they are not. When they are in loops, I need to pass variables that access the variable as defined in the foreach loop.

recipes.blade

...
@include('text'
 [
    'value' => $recipe->title
 ])
@include('nested'
[
    'value' => $recipe->directions
 ])
...

container.blade

<ul>
  @foreach ($value as $instance)
       <li>
       @yield('contents')
       </li>
  @endforeach
</ul>

nested.blade

@extends('container')

@section('contents')
  @include('text'
    [
        'value' => $instance->description
     ])
  @include('text'
    [
        'value' => $instance->content
     ])
@endsection

Text.blade

<p></p>

I need to pass $instance as part of the variable. I also tried checking in the text.blade file if a loop is in progress - if I could determine that I could add an if/else statement to handle the issue. Both create an error, as Laravel sees that neither $instance nor $loop are defined.

I would prefer a way to force Laravel to compile the entire file, before checking if variables exist (right now it seems to be checking in each partial file before compiling). If it would check in the context of the entire file it wouldn't load text.blade until it has opened the loop, making $instance available. A way to pass the $instance variable without an error AND allowing it to be used in the view would be another way around the issue.

How can I use the same partial inside and outside loops?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/307TaOV
via IFTTT

Aucun commentaire:

Enregistrer un commentaire