Framework: Cannot @include the same view multiple times if it uses inheritance

Created on 29 Sep 2016  路  6Comments  路  Source: laravel/framework

  • Laravel Version: 5.3
  • PHP Version: 7.0.11
  • Database Driver & Version: none

    Description:

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.

Steps To Reproduce:

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>

Expected results:

<p>Hello, Town!</p>
<p>Hello, World!</p>

Actual results:

<p>Hello, Town!</p>
<p>Hello, Town!</p>

Most helpful comment

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

All 6 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixsanz picture felixsanz  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

YannPl picture YannPl  路  3Comments

digirew picture digirew  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments