Framework: Blade sections override issue

Created on 12 Apr 2017  路  5Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.18
  • PHP Version: 7.1.3
  • Database Driver & Version: None

Description:

Blade seems to be unable to override sections that were already defined in the extended layout. It seems like Blade is first including / executing the extended layout before overriding the sections.

Steps To Reproduce:

You need 3 views:

index.blade.php

@extends('layout')
@section('remove-me', '')

layout.blade.php

@section('remove-me')
    @include('include')
@show

include.blade.php

{{ $iDoNotExist }}

Expected behavior

Empty HTML page

Actual behavior

Undefined variable: iDoNotExist

Most helpful comment

Well, the point is - in the example it should not get included at all, because I overwrite the section in the beginning.

All 5 comments

Give include.blade.php the variable it needs

Well, the point is - in the example it should not get included at all, because I overwrite the section in the beginning.

Now I got it.

Yes blade compiles all section content in order to be able to replace @parent with the parent section content, so yes the all section code is actually included using PHP's include, section content are collected in a stack, and then on render Blade manages the output based on the different sections you have.

Closing since it's not actually a bug, this behaviour is by design, sections are compiled in order to be able to use the @parent directive.

Was this page helpful?
0 / 5 - 0 ratings