Framework: Blade - Including templates that extend other templates causes issues.

Created on 24 Apr 2013  路  9Comments  路  Source: laravel/framework

When looping through an array in a Blade template, if you include a different template that extends another template, the first item in the array is used each time in that include.

This doesn't happen if the view that you include does not have an extend.

Here is a reduced case:
http://paste.laravel.com/p3P

So, from the example above, the @include('extend') will display 5 times for the first item and the @include('noextend') will display 5 times for the appropriate item.

I have a feeling that I'm seriously misusing the Blade compiler with this, but I'm not really sure of what would be a better approach while still using primarily Blade for view construction.

Most helpful comment

OK, I think I have a solution here. Instead of using @endsection or @stop in your included template that calls @section('stuff'), use @overwrite:

@section('stuff')
     Stuff goes here...
@overwrite

All 9 comments

I had the same problem. Also didn't find any good solution for this.

Just a note related to this.

Looks like nested extends break the output buffering used by PHPUnit as well. This occurs if the view with nested extends is returned as the response in a unit test.

As per https://github.com/symfony/symfony/issues/2531, adding this to your test class will fix the issue.

public function tearDown()
{
    parent::tearDown();

    // workaround for https://github.com/symfony/symfony/issues/2531
    if (ob_get_length() == 0 ) {
        ob_start();
    }
}

OK, I think I have a solution here. Instead of using @endsection or @stop in your included template that calls @section('stuff'), use @overwrite:

@section('stuff')
     Stuff goes here...
@overwrite

Works perfectly. Didn't even know that method existed.

@WMeldon it didn't, @taylorotwell added it for you ;P

@brunogaspar That would explain why I wasn't able to find it in the documentation OR the source code.

This will definitely need to be added to the docs.

How can I study about the different between @overwrite @stop @endsection @show?
Are there any docs about that?

Someone, please doc this.

Solved my issue, would be nice if the differences between the end tags are cleared up a bit. Please and thank you!

The closest to complete documentation I could find was this :: http://laravel-recipes.com/recipes/248/knowing-all-the-blade-template-commands

Was this page helpful?
0 / 5 - 0 ratings