Using @include to include the same view multiple times in a page, for example to render many similar parts (many text widgets in a form, many banners, etc.) doesn't work if the included view uses @extends: all the instances come out with the contents of the first one.
welcome.blade.php:
@include('hello', ['name' => 'Town'])
@include('hello', ['name' => 'World'])
hello.blade.php:
@extends('greeting')
@section('content')
Hello, {{ $name }}!
@stop
greeting.blade.php:
<p>@yield('content')</p>
<p>Hello, Town!</p>
<p>Hello, World!</p>
<p>Hello, Town!</p>
<p>Hello, Town!</p>
I believe you can only have one section called content
Can you try to give the section another name and see if that works?
My use case is composing a form from reusable widgets. If my form has two or more text inputs, they should be rendered with the same view, because they are the same exact widget. Only the variables (the view data) differ.
How can I use different section names if it's always the same view, with different data?
Since this will render section content multiple times you need to overwrite the output because instead it'll keep using the first rendering.
@extends('greeting')
@section('content')
Hello, {{ $name }}!
@overwrite
This will solve your issue, notice @overwrite
I'll be damned! I only knew about @show and @stop, but there's also @overwrite and @append!
Time to go read the source code. Thanks.
Welcome :)
Thanks themsaid!
Most helpful comment
Since this will render section
contentmultiple times you need to overwrite the output because instead it'll keep using the first rendering.This will solve your issue, notice
@overwrite